trading_agent.agent.conversation
¶
A desk's conversation with its model: the system prompt and the messages so far.
Messages are appended, and only ever changed in two ways: compression replaces the whole history at once with a summary and the current turn, which is a fresh conversation as far as the model can tell, and older pictures are taken out so that only the newest few are sent with every model call.
The conversation also remembers how many tokens the model said it held at the last call, so the compressor can tell how full the context window is.
Typical usage example:
conversation = Conversation('You are a trading desk.') conversation.append(ConversationMessage.user('Hello'))
Conversation
¶
An append-only list of messages with a fixed system prompt.
Attributes:
| Name | Type | Description |
|---|---|---|
system_prompt |
The instructions sent before every message. |
|
measured_tokens |
The tokens the model reported for the conversation at its last call, or 0 before any call. |
Creates an empty conversation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
system_prompt
|
str
|
The instructions sent before every message. |
required |
Source code in src/trading_agent/agent/conversation.py
append
¶
Adds a message to the end.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
ConversationMessage
|
The message to add. |
required |
copy
¶
A separate conversation with the same prompt and messages, which can be extended without changing this one.
Returns:
| Type | Description |
|---|---|
Conversation
|
The copy. |
Source code in src/trading_agent/agent/conversation.py
record_usage
¶
Remembers how many tokens the model reported at the call that just finished.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tokens
|
int
|
The prompt tokens plus the reply's tokens. |
required |
Source code in src/trading_agent/agent/conversation.py
estimated_tokens
¶
How many tokens the next call will send, as closely as can be told now.
It is the last measured count plus an estimate for the messages added since, such as tool results: three characters per token of text, and 1500 tokens for each picture.
Returns:
| Type | Description |
|---|---|
int
|
The estimate. |
Source code in src/trading_agent/agent/conversation.py
keep_newest_pictures
¶
Takes every picture but the newest few out of the conversation, leaving a note in each one's place.
Every message is sent again with each model call, so a picture left in the history costs about 1500 tokens on every later call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
count
|
int
|
How many of the newest pictures to keep. |
required |
Returns:
| Type | Description |
|---|---|
int
|
How many pictures were taken out. |
Source code in src/trading_agent/agent/conversation.py
restart
¶
Replaces the whole history at once, for compression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
new_messages
|
Sequence[ConversationMessage]
|
The messages the conversation starts again with. |
required |