agent
Agent
Bases: Generic[AgentInputT, AgentOutputT]
Source code in agenty/agent/base.py
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 | |
model_name
property
Get the name of the current model.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The model name |
Raises:
| Type | Description |
|---|---|
ValueError
|
If no model is set |
pai_agent
property
system_prompt_rendered
property
Render the system prompt with the current template context.
Returns:
| Name | Type | Description |
|---|---|---|
str |
str
|
The rendered system prompt |
reset()
Reset the agent to its initial state.
Source code in agenty/agent/base.py
236 237 238 | |
run(input_data, name=None)
async
Run the agent with the provided input.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_data
|
Optional[AgentInputT]
|
The input data for the agent to process |
required |
Returns:
| Type | Description |
|---|---|
AgentOutputT
|
The processed output data |
Source code in agenty/agent/base.py
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 | |
template_context()
Get a dictionary of instance variables for use in templates. By default, this includes all variables that start with an uppercase letter.
Returns:
| Type | Description |
|---|---|
Dict[str, Any]
|
Dict[str, Any]: Dictionary of variables |
Source code in agenty/agent/base.py
228 229 230 231 232 233 234 | |
AgentUsage
Bases: MutableMapping[str, Usage]
Tracks usage statistics for multiple models in an agent.
A dictionary-like container that maps model names to their usage statistics. Automatically creates new Usage entries for unknown models and provides aggregated statistics across all models.
Source code in agenty/agent/usage.py
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 | |
request_tokens
property
Get the total number of request tokens across all models.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of tokens used in requests across all models |
requests
property
Get the total number of requests across all models.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of API requests made across all models |
response_tokens
property
Get the total number of response tokens across all models.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of tokens in model responses across all models |
total_tokens
property
Get the total number of tokens across all models.
Returns:
| Name | Type | Description |
|---|---|---|
int |
int
|
Total number of tokens used (requests + responses) across all models |
AgentUsageLimits
Bases: MutableMapping[str, UsageLimits]
Tracks usage limits for multiple models in an agent.
A dictionary-like container that maps model names to their UsageLimits. Automatically creates new UsageLimits entries for unknown models.
Source code in agenty/agent/usage.py
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
ChatHistory
Bases: MutableSequence[ChatMessage]
Manages conversation history for an AI agent.
Implements MutableSequence for list-like access to message history. Handles conversation turns and optional history length limits.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
max_messages
|
int
|
Max messages to keep (-1 for unlimited) |
-1
|
messages
|
Optional[Sequence[ChatMessage]]
|
Optional initial messages |
None
|
Source code in agenty/agent/chat_history.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 | |
add(role, content, name=None, inject_name=False)
Add a message to history.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
role
|
Role
|
Message sender's role |
required |
content
|
AgentIO
|
Message content |
required |
name
|
Optional[str]
|
Optional sender name |
None
|
Note
Automatically initializes a new turn if none is active. Messages added in the same turn share a turn_id.
Source code in agenty/agent/chat_history.py
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
clear()
Clear all messages from memory and reset the current turn.
This method removes all stored messages and resets the current_turn_id to None, effectively starting fresh with an empty memory state.
Source code in agenty/agent/chat_history.py
196 197 198 199 200 201 202 203 | |
end_turn()
End current conversation turn.
After calling this, the next message added will start a new turn with a new turn_id. This helps organize messages into logical groups or turns in the conversation.
Note
This does not remove any messages, it only resets the current_turn_id to None so that the next message will start a new turn.
Source code in agenty/agent/chat_history.py
216 217 218 219 220 221 222 223 224 225 226 227 | |
initialize_turn()
Start a new conversation turn with a fresh UUID.
This method generates a new UUID for the current turn and sets it as the current_turn_id. All messages added after initializing a turn will share this turn_id until end_turn() is called.
Source code in agenty/agent/chat_history.py
159 160 161 162 163 164 165 | |
insert(index, value)
Insert message at index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Position to insert at |
required |
value
|
ChatMessage
|
Message to insert |
required |
Note
May trigger history culling if max_messages is exceeded
Source code in agenty/agent/chat_history.py
331 332 333 334 335 336 337 338 339 340 341 342 | |
to_openai(ctx={})
Get history in OpenAI API format.
Converts all messages in memory to the format expected by OpenAI's Chat API. Each message is rendered with the provided template context before conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
dict[str, Any]
|
Template context dictionary for variable substitution. Used to render any template variables in message content. |
{}
|
Returns:
| Type | Description |
|---|---|
list[ChatCompletionMessageParam]
|
list[ChatCompletionMessageParam]: Messages formatted for OpenAI API, with each message containing role, content, and optional name fields. |
Source code in agenty/agent/chat_history.py
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 | |
to_pydantic_ai(ctx={})
Get history in Pydantic-AI format.
Converts all messages in memory to the format expected by Pydantic-AI. Each message is rendered with the provided template context before conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
dict[str, Any]
|
Template context dictionary for variable substitution. Used to render any template variables in message content. |
{}
|
Returns:
| Type | Description |
|---|---|
list[ModelMessage]
|
list[ModelMessage]: Messages formatted for Pydantic-AI, with each message converted to the appropriate ModelMessage subtype based on its role (ModelRequest for user/system, ModelResponse for assistant). |
Raises:
| Type | Description |
|---|---|
ValueError
|
If a message has an unsupported role that cannot be converted to a Pydantic-AI message type. |
Source code in agenty/agent/chat_history.py
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |