Overview
TheAgentContext class holds execution data from an agent run, including token usage, costs, trajectory details, and custom metadata. Agents populate this object during execution.
Import: from harbor.models.agent.context import AgentContext
Fields
int | None
default:"None"
Total number of input tokens used including cache tokens.
int | None
default:"None"
Number of cache tokens used (subset of input tokens).
int | None
default:"None"
Total number of output tokens generated.
float | None
default:"None"
Total cost in USD for the agent execution.
list[RolloutDetail] | None
default:"None"
Detailed information about each rollout trajectory including token IDs, loss masks, and logprobs. Each element represents one trajectory. For a linear chat history, there is only one rollout trajectory.
dict[str, Any] | None
default:"None"
Additional metadata about the agent execution. Use this for custom tracking data.
Methods
is_empty
bool
True if all fields are None, False otherwise.
RolloutDetail Type
TheRolloutDetail TypedDict contains detailed trajectory information for reinforcement learning and analysis:
list[list[int]]
Each element contains full prompt token IDs for that turn, including previous chat history if applicable.
list[list[int]]
Each element contains response token IDs for that turn.
list[list[float]]
Each element contains logprobs corresponding to completion_token_ids for that turn.
Usage Examples
Basic Token Tracking
Adding Custom Metadata
With Rollout Details for RL
Checking Context State
In Agent Implementation
Serialization
AgentContext is a Pydantic model and supports JSON serialization:Related Types
- BaseAgent - Agent interface that populates AgentContext
- TrialResult - Contains AgentContext as agent_result field
- RolloutDetail - Trajectory data structure