trading_agent.agent.context_usage
¶
How full a desk's context window is, broken down by what fills it.
Claude Code measures its own window and names the categories itself, such as "Messages", "System tools" and "Free space". The Ollama and OpenAI backends have no such report, so their runner estimates the same kind of breakdown from the conversation it keeps: the system prompt, the tool definitions, the messages and the space left.
Typical usage example:
usage = ContextUsage.estimate('qwen', 'qwen3.8:27b', 32768, conversation, definitions) usage.to_payload()
ContextCategory
dataclass
¶
One part of the context window.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
What fills this part, such as "Messages". |
tokens |
int
|
How many tokens it holds. |
deferred |
bool
|
True for something listed but not loaded into the window, such as a deferred tool. |
ContextUsage
dataclass
¶
A snapshot of one backend's context window.
Attributes:
| Name | Type | Description |
|---|---|---|
backend |
str
|
The backend's name in trading_agent.toml. |
model |
str
|
The model's name on its server. |
total_tokens |
int
|
How many tokens the window holds. |
context_window |
int
|
How many tokens it can hold. |
categories |
tuple[ContextCategory, ...]
|
What fills it, in the order to show them. |
measured |
bool
|
True when the model's own tool counted the tokens, False for an estimate. |
recorded_at |
str
|
When the snapshot was taken, as ISO text. |
from_claude
classmethod
¶
Builds a snapshot from Claude Code's get_context_usage report.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str
|
The backend's name in trading_agent.toml. |
required |
report
|
Mapping[str, Any]
|
The report, with categories, totalTokens, rawMaxTokens and model. |
required |
Returns:
| Type | Description |
|---|---|
ContextUsage
|
The snapshot. |
Source code in src/trading_agent/agent/context_usage.py
estimate
classmethod
¶
Estimates a snapshot from a conversation the agent loop keeps.
The system prompt, the tool definitions and the messages are first counted roughly, at three characters per token and 1500 tokens per picture. Once the model has reported how many tokens it read, the three rough counts are scaled so that they add up to that total, because the rough rate is far off for some text, such as the JSON of tool definitions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
backend
|
str
|
The backend's name in trading_agent.toml. |
required |
model
|
str
|
The model's name on its server. |
required |
context_window
|
int
|
How many tokens the model is given. |
required |
conversation
|
Conversation
|
The conversation so far. |
required |
tool_definitions
|
Sequence[Mapping[str, Any]]
|
The tools the model is offered. |
required |
Returns:
| Type | Description |
|---|---|
ContextUsage
|
The estimated snapshot. |
Source code in src/trading_agent/agent/context_usage.py
to_payload
¶
Describes the snapshot for the gateway's API.
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
The snapshot as plain JSON-ready values. |