trading_agent.agent.agent_turn_runner
¶
The turn runner that drives a model through the agent loop.
It keeps the desk's conversation between turns, so the desk remembers what was said earlier, and rebuilds the system prompt before every turn, so an edit to a workspace file takes effect on the next turn.
Typical usage example:
runner = AgentTurnRunner(agent_loop, prompt_builder) result = await runner.run_turn(trigger)
AgentTurnRunner
¶
Bases: TurnRunner
Runs each trigger as a turn of the agent loop, in one ongoing conversation.
Attributes:
| Name | Type | Description |
|---|---|---|
conversation |
The conversation so far. |
Creates the runner with an empty conversation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
agent_loop
|
AgentLoop
|
The loop to run each turn with. |
required |
prompt_builder
|
PromptBuilder
|
Builds the desk's system prompt. |
required |
context_window
|
int
|
How many tokens the model is given, for context_usage; 0 when it is not known. |
0
|
Source code in src/trading_agent/agent/agent_turn_runner.py
run_turn
async
¶
Refreshes the system prompt and runs one turn of the agent loop.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trigger
|
Trigger
|
The reason for the turn. |
required |
Returns:
| Type | Description |
|---|---|
TurnResult
|
The model's final text. |
Source code in src/trading_agent/agent/agent_turn_runner.py
compact
async
¶
Flushes and compresses the conversation now, for the /compact command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
trigger
|
Trigger
|
The command's trigger, used to tag events. |
required |
Returns:
| Type | Description |
|---|---|
str
|
What happened, for the user. |
Raises:
| Type | Description |
|---|---|
BackendUnavailableError
|
The model could not be reached. |
Source code in src/trading_agent/agent/agent_turn_runner.py
active_model
¶
The model the loop's backend uses now.
Returns:
| Type | Description |
|---|---|
str | None
|
The model's name. |
use_model
¶
Switches the loop's backend to another model on the same server, keeping the conversation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str | None
|
The model's name, or None for the configured model. |
required |
Source code in src/trading_agent/agent/agent_turn_runner.py
side_question
async
¶
Asks the backend once, with a copy of the conversation and no tools, so nothing is kept.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
question
|
str
|
The user's question. |
required |
Returns:
| Type | Description |
|---|---|
str
|
The model's answer. |
Raises:
| Type | Description |
|---|---|
BackendUnavailableError
|
The backend could not answer. |
Source code in src/trading_agent/agent/agent_turn_runner.py
thinking
¶
The thinking level of the loop's backend.
Returns:
| Type | Description |
|---|---|
str | None
|
The level, or None when the backend has no thinking setting. |
set_thinking
¶
Changes the thinking level of the loop's backend, keeping the conversation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
level
|
str | None
|
One of turn_runner.THINKING_LEVELS, or None for the configured setting. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the backend applied it, False when it has no thinking setting. |
Source code in src/trading_agent/agent/agent_turn_runner.py
context_usage
¶
Estimates how full the model's context window is.
Returns:
| Type | Description |
|---|---|
ContextUsage | None
|
The estimate, or None when the context window is not known. |