Skip to content

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.

Note

These types are immutable (frozen dataclasses) to ensure trajectory data cannot be modified after capture, maintaining audit integrity.

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.

ATTRIBUTE DESCRIPTION
name

Tool or function name that was called.

TYPE: str

arguments

Dictionary of arguments passed to the tool.

TYPE: dict[str, Any]

result

Return value from the tool execution.

TYPE: Any

timestamp

Relative time in seconds from evaluation start.

TYPE: float

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
@dataclass(frozen=True, slots=True)
class ToolCallRecord:
    """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.

    Attributes:
        name (str): Tool or function name that was called.
        arguments (dict[str, Any]): Dictionary of arguments passed to the tool.
        result (Any): Return value from the tool execution.
        timestamp (float): Relative time in seconds from evaluation start.

    Examples:
        ```python
        record = ToolCallRecord(
            name="get_weather",
            arguments={"city": "Paris"},
            result={"temp": 22, "condition": "sunny"},
            timestamp=0.123,
        )
        ```
    """

    name: str
    arguments: dict[str, Any]
    result: Any
    timestamp: float

TokenUsage dataclass

Token usage statistics from LLM calls.

Tracks token consumption for monitoring costs and performance of language model interactions during agent execution.

ATTRIBUTE DESCRIPTION
input_tokens

Number of tokens in the prompt/context.

TYPE: int

output_tokens

Number of tokens generated in the response.

TYPE: int

total_tokens

Sum of input_tokens and output_tokens.

TYPE: int

Examples:

usage = TokenUsage(input_tokens=150, output_tokens=50, total_tokens=200)
Source code in src/gepa_adk/domain/trajectory.py
@dataclass(frozen=True, slots=True)
class TokenUsage:
    """Token usage statistics from LLM calls.

    Tracks token consumption for monitoring costs and performance of
    language model interactions during agent execution.

    Attributes:
        input_tokens (int): Number of tokens in the prompt/context.
        output_tokens (int): Number of tokens generated in the response.
        total_tokens (int): Sum of input_tokens and output_tokens.

    Examples:
        ```python
        usage = TokenUsage(input_tokens=150, output_tokens=50, total_tokens=200)
        ```
    """

    input_tokens: int
    output_tokens: int
    total_tokens: int

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.

ATTRIBUTE DESCRIPTION
tool_calls

Immutable sequence of tool invocations during execution.

TYPE: tuple[ToolCallRecord, ...]

state_deltas

Sequence of state changes (session state updates).

TYPE: tuple[dict[str, Any], ...]

token_usage

Optional token consumption metrics from LLM calls.

TYPE: TokenUsage | None

final_output

Final text response from the agent.

TYPE: str

error

Error message if execution failed, None otherwise.

TYPE: str | None

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,
)
Note

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
@dataclass(frozen=True, slots=True)
class ADKTrajectory:
    """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.

    Attributes:
        tool_calls (tuple[ToolCallRecord, ...]): Immutable sequence of tool
            invocations during execution.
        state_deltas (tuple[dict[str, Any], ...]): Sequence of state changes
            (session state updates).
        token_usage (TokenUsage | None): Optional token consumption metrics
            from LLM calls.
        final_output (str): Final text response from the agent.
        error (str | None): Error message if execution failed, None otherwise.

    Examples:
        ```python
        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,
        )
        ```

    Note:
        All fields use immutable types (tuples, not lists) to prevent
        accidental modification of captured trace data.
    """

    tool_calls: tuple[ToolCallRecord, ...]
    state_deltas: tuple[dict[str, Any], ...]
    token_usage: TokenUsage | None
    final_output: str
    error: str | None

MultiAgentTrajectory dataclass

Execution trace from multi-agent pipeline evaluation.

Captures individual agent trajectories and overall pipeline metrics.

ATTRIBUTE DESCRIPTION
agent_trajectories

Mapping of agent name to trajectory.

TYPE: dict[str, ADKTrajectory]

pipeline_output

Final output from the primary agent.

TYPE: str

total_token_usage

Aggregated token usage across all agents.

TYPE: TokenUsage | None

error

Error message if pipeline execution failed.

TYPE: str | None

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,
)
Note

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
@dataclass(frozen=True, slots=True)
class MultiAgentTrajectory:
    """Execution trace from multi-agent pipeline evaluation.

    Captures individual agent trajectories and overall pipeline metrics.

    Attributes:
        agent_trajectories (dict[str, ADKTrajectory]): Mapping of agent name to trajectory.
        pipeline_output (str): Final output from the primary agent.
        total_token_usage (TokenUsage | None): Aggregated token usage across all agents.
        error (str | None): Error message if pipeline execution failed.

    Examples:
        Creating a multi-agent trajectory:

        ```python
        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,
        )
        ```

    Note:
        All fields use immutable types to prevent accidental modification
        of captured trace data. agent_trajectories maps agent names to
        their individual ADKTrajectory records.
    """

    agent_trajectories: dict[str, ADKTrajectory]
    pipeline_output: str
    total_token_usage: TokenUsage | None
    error: str | None = None