Callbacks
Cost Optimization
Budget management and image retention for cost optimization
Cost Optimization Callbacks
Optimize agent costs with budget management and image retention callbacks.
Budget Manager Callbacks Example
from agent.callbacks import BudgetManagerCallback
agent = ComputerAgent(
model="anthropic/claude-3-5-sonnet-20241022",
tools=[computer],
callbacks=[
BudgetManagerCallback(
max_budget=5.0, # $5 limit
reset_after_each_run=False,
raise_error=True
)
]
)
Budget Manager Shorthand
agent = ComputerAgent(
model="anthropic/claude-3-5-sonnet-20241022",
tools=[computer],
max_trajectory_budget=5.0 # Auto-adds BudgetManagerCallback
)
Or with options:
agent = ComputerAgent(
model="anthropic/claude-3-5-sonnet-20241022",
tools=[computer],
max_trajectory_budget={"max_budget": 5.0, "raise_error": True}
)
Image Retention Callbacks Example
from agent.callbacks import ImageRetentionCallback
agent = ComputerAgent(
model="anthropic/claude-3-5-sonnet-20241022",
tools=[computer],
callbacks=[
ImageRetentionCallback(only_n_most_recent_images=3)
]
)
Image Retention Shorthand
agent = ComputerAgent(
model="anthropic/claude-3-5-sonnet-20241022",
tools=[computer],
only_n_most_recent_images=3 # Auto-adds ImageRetentionCallback
)
Combined Cost Optimization
agent = ComputerAgent(
model="anthropic/claude-3-5-sonnet-20241022",
tools=[computer],
max_trajectory_budget=5.0, # Budget limit
only_n_most_recent_images=3, # Image retention
trajectory_dir="trajectories" # Track spending
)
Budget Manager Options
max_budget
: Dollar limit for trajectoryreset_after_each_run
: Reset budget per run (default: True)raise_error
: Raise exception vs. graceful stop (default: False)