trading_agent.agent.loop_guard
¶
Stops a model from calling the same tool with the same arguments over and over in one turn.
Small models sometimes get stuck repeating one call. For a trading agent that could mean sending the same order again and again, so the guard refuses a call once it has been made too many times in a turn. The risk engine (milestone 9) also limits orders per minute; the guard catches the loop earlier and for every tool.
Typical usage example:
guard = LoopGuard(maximum_identical_calls=3) guard.start_turn() if guard.allows(call): ...
LoopGuard
¶
Counts identical tool calls within the current turn.
Creates the guard.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
maximum_identical_calls
|
int
|
How many times one tool may be called with the same arguments in a turn. |
required |
Source code in src/trading_agent/agent/loop_guard.py
start_turn
¶
allows
¶
Records a call and says whether it may run.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
call
|
ToolCall
|
The call the model made. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
False once the same tool and arguments have been seen more than the maximum in this turn. |
Source code in src/trading_agent/agent/loop_guard.py
refusal_text
¶
The message sent to the model instead of running a refused call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
call
|
ToolCall
|
The refused call. |
required |
Returns:
| Type | Description |
|---|---|
str
|
An explanation the model can act on. |