trading_agent.agent.context_compressor
¶
Compresses a conversation into a summary when it fills too much of the model's context window.
A model can only read so many tokens at once: 32,768 for Gemma and Qwen as configured. When a conversation passes 70% of that, the agent loop first gives the desk a silent memory-flush turn to save what matters with add_note or Memory.md, and then this compressor asks the same model to summarise the older messages. The conversation starts again from that summary, followed by the current turn's messages, which are kept word for word.
If a single long turn passes 85% between tool rounds, the compressor runs at once without the flush, because Ollama would otherwise drop the oldest text, the instructions included, without saying so.
Typical usage example:
compressor = ContextCompressor('main', 32768, 0.70, 0.85, event_bus) if compressor.is_full(conversation): await compressor.compress(conversation, backend, len(conversation.messages), 'threshold', trigger_id)
FLUSH_REQUEST
module-attribute
¶
FLUSH_REQUEST = "[Memory flush: your conversation is about to be compressed into a summary to free up context. Before it is, save anything you will need later that is not saved yet: record lessons with add_note, and put lasting facts in Memory.md. Do not place, change or cancel orders in this turn. Reply with one short line saying what you saved, or 'Nothing to save'.]"
SUMMARY_FOCUS
module-attribute
¶
SUMMARY_FOCUS = "open positions and orders with their ids, prices and quantities; the decisions made and why; the user's instructions and preferences; plans and pending tasks; errors and what was learned from them"
ContextCompressor
¶
Decides when one backend's conversation is too full, and compresses it.
Creates the compressor.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
desk_name
|
str
|
The desk, used to tag events. |
required |
context_window
|
int
|
How many tokens the model is given. |
required |
threshold
|
float
|
The share of the window at which to compress before a turn, such as 0.70. |
required |
emergency_threshold
|
float
|
The share at which to compress in the middle of a turn, such as 0.85. |
required |
event_bus
|
EventBus
|
Where each compression is published. |
required |
Source code in src/trading_agent/agent/context_compressor.py
is_full
¶
Says whether the conversation has reached the normal threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conversation
|
Conversation
|
The conversation. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when it should be compressed before the next turn. |
Source code in src/trading_agent/agent/context_compressor.py
is_overflowing
¶
Says whether the conversation has reached the emergency threshold.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conversation
|
Conversation
|
The conversation. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when it must be compressed before the next model call. |
Source code in src/trading_agent/agent/context_compressor.py
compress
async
¶
Summarises the messages before keep_from and restarts the conversation from the summary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
conversation
|
Conversation
|
The conversation to compress. |
required |
backend
|
ModelBackend
|
The model that writes the summary. |
required |
keep_from
|
int
|
The index of the first message to keep word for word, normally the current turn's user message. |
required |
reason
|
str
|
"threshold", "emergency" or "command", for the event. |
required |
trigger_id
|
str
|
The trigger of the turn that caused it, for the event. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True when the conversation was compressed, False when there was nothing older to summarise. |
Raises:
| Type | Description |
|---|---|
BackendUnavailableError
|
The model could not write the summary. |