Trajectory
trajectory ¶
Trajectory and trace types for agent execution tracking.
This module defines data structures for capturing agent execution traces, including tool calls, state changes, and token usage metrics.
Examples:
Creating a trajectory from an agent evaluation:
from gepa_adk.domain.trajectory import ADKTrajectory, TokenUsage, ToolCallRecord
trajectory = ADKTrajectory(
tool_calls=(ToolCallRecord("search", {"q": "AI"}, ["result"], 0.1),),
state_deltas=(),
token_usage=TokenUsage(input_tokens=100, output_tokens=50, total_tokens=150),
final_output="Search complete.",
error=None,
)
Notes
These types are immutable (frozen dataclasses) to ensure trajectory data cannot be modified after capture, maintaining audit integrity. Each type has to_dict() and from_dict() for JSON-safe serialisation: tuples become lists and back, and a tool-call argument, tool-call result or state-delta value that json cannot encode is stored as its str().
See Also
gepa_adk.ports.adapter.EvaluationBatch: Adapter protocol that produces trajectory data during evaluation.
ToolCallRecord dataclass ¶
Record of a single tool call during agent execution.
Captures the invocation details of a tool/function call made by an agent during evaluation, including arguments, results, and timing information. to_dict() and from_dict() convert it to and from a JSON-safe dict.
| ATTRIBUTE | DESCRIPTION |
|---|---|
name | Tool or function name that was called. TYPE: |
arguments | Dictionary of arguments passed to the tool. TYPE: |
result | Return value from the tool execution. TYPE: |
timestamp | Relative time in seconds from evaluation start. TYPE: |
Examples:
record = ToolCallRecord(
name="get_weather",
arguments={"city": "Paris"},
result={"temp": 22, "condition": "sunny"},
timestamp=0.123,
)
Source code in src/gepa_adk/domain/trajectory.py
to_dict ¶
Serialise this record to a JSON-safe dict.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Dict with |
dict[str, Any] | An argument value or result that JSON cannot encode is stored as |
dict[str, Any] | its |
Source code in src/gepa_adk/domain/trajectory.py
from_dict classmethod ¶
from_dict(data: dict[str, Any]) -> ToolCallRecord
Rebuild a record from to_dict() output.
| PARAMETER | DESCRIPTION |
|---|---|
data | Dict produced by TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ToolCallRecord | The reconstructed record. |
| RAISES | DESCRIPTION |
|---|---|
KeyError | If a field is missing from |
Source code in src/gepa_adk/domain/trajectory.py
TokenUsage dataclass ¶
Token usage statistics from LLM calls.
Tracks token consumption for monitoring costs and performance of language model interactions during agent execution. to_dict() and from_dict() convert it to and from a JSON-safe dict.
| ATTRIBUTE | DESCRIPTION |
|---|---|
input_tokens | Number of tokens in the prompt/context. TYPE: |
output_tokens | Number of tokens generated in the response. TYPE: |
total_tokens | Sum of input_tokens and output_tokens. TYPE: |
Examples:
Source code in src/gepa_adk/domain/trajectory.py
to_dict ¶
Serialise these counts to a JSON-safe dict.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Dict with |
dict[str, Any] |
|
Source code in src/gepa_adk/domain/trajectory.py
from_dict classmethod ¶
from_dict(data: dict[str, Any]) -> TokenUsage
Rebuild token counts from to_dict() output.
| PARAMETER | DESCRIPTION |
|---|---|
data | Dict produced by TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
TokenUsage | The reconstructed counts. |
| RAISES | DESCRIPTION |
|---|---|
KeyError | If a field is missing from |
Source code in src/gepa_adk/domain/trajectory.py
ADKTrajectory dataclass ¶
Execution trace from ADK agent evaluation.
Captures complete execution details from a single agent evaluation run, including all tool calls, state changes, token usage, and final output. This data enables debugging, optimization, and reflection-based learning. to_dict() and from_dict() convert it to and from a JSON-safe dict, turning tuples into lists and back.
| ATTRIBUTE | DESCRIPTION |
|---|---|
tool_calls | Immutable sequence of tool invocations during execution. TYPE: |
state_deltas | Sequence of state changes (session state updates). TYPE: |
token_usage | Optional token consumption metrics from LLM calls. TYPE: |
final_output | Final text response from the agent. TYPE: |
error | Error message if execution failed, None otherwise. TYPE: |
Examples:
trajectory = ADKTrajectory(
tool_calls=(ToolCallRecord("search", {"query": "AI"}, ["result1"], 0.1),),
state_deltas=({"search_count": 1},),
token_usage=TokenUsage(100, 50, 150),
final_output="Based on the search...",
error=None,
)
Notes
All fields use immutable types (tuples, not lists) to prevent accidental modification of captured trace data.
Source code in src/gepa_adk/domain/trajectory.py
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 | |
to_dict ¶
Serialise this trace to a JSON-safe dict.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Dict with |
dict[str, Any] |
|
dict[str, Any] |
|
dict[str, Any] | as its |
Source code in src/gepa_adk/domain/trajectory.py
from_dict classmethod ¶
from_dict(data: dict[str, Any]) -> ADKTrajectory
Rebuild a trace from to_dict() output.
| PARAMETER | DESCRIPTION |
|---|---|
data | Dict produced by TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
ADKTrajectory | The reconstructed trace, with lists turned back into tuples. |
| RAISES | DESCRIPTION |
|---|---|
KeyError | If a field is missing from |
Source code in src/gepa_adk/domain/trajectory.py
MultiAgentTrajectory dataclass ¶
Execution trace from multi-agent pipeline evaluation.
Captures individual agent trajectories and overall pipeline metrics. to_dict() and from_dict() convert it to and from a JSON-safe dict, nesting each agent's ADKTrajectory dict.
| ATTRIBUTE | DESCRIPTION |
|---|---|
agent_trajectories | Mapping of agent name to trajectory. TYPE: |
pipeline_output | Final output from the primary agent. TYPE: |
total_token_usage | Aggregated token usage across all agents. TYPE: |
error | Error message if pipeline execution failed. TYPE: |
Examples:
Creating a multi-agent trajectory:
from gepa_adk.domain.trajectory import (
MultiAgentTrajectory,
ADKTrajectory,
TokenUsage,
)
trajectory = MultiAgentTrajectory(
agent_trajectories={
"generator": ADKTrajectory(...),
"critic": ADKTrajectory(...),
},
pipeline_output="Generated code output",
total_token_usage=TokenUsage(200, 100, 300),
error=None,
)
Notes
All fields use immutable types to prevent accidental modification of captured trace data. agent_trajectories maps agent names to their individual ADKTrajectory records.
Source code in src/gepa_adk/domain/trajectory.py
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 | |
to_dict ¶
Serialise this pipeline trace to a JSON-safe dict.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any] | Dict with |
dict[str, Any] |
|
dict[str, Any] |
|
Source code in src/gepa_adk/domain/trajectory.py
from_dict classmethod ¶
from_dict(data: dict[str, Any]) -> MultiAgentTrajectory
Rebuild a pipeline trace from to_dict() output.
| PARAMETER | DESCRIPTION |
|---|---|
data | Dict produced by TYPE: |
| RETURNS | DESCRIPTION |
|---|---|
MultiAgentTrajectory | The reconstructed pipeline trace. |
| RAISES | DESCRIPTION |
|---|---|
KeyError | If a required field is missing from |