Repository navigation
agent-core: per-run dollar cap for hands (max_cost_usd) #78
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,6 @@ | ||
| use iii_sdk::{III, InitOptions, RegisterFunction, RegisterTriggerInput, TriggerRequest, register_worker}; | ||
| use iii_sdk::error::IIIError; | ||
| use llm_budget::AgentBudget; | ||
| use serde_json::{json, Value}; | ||
| use std::time::Instant; | ||
|
|
||
|
|
@@ -131,6 +132,11 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> { | |
|
|
||
| async fn agent_chat(iii: &III, req: ChatRequest) -> Result<Value, IIIError> { | ||
| let start = Instant::now(); | ||
| let budget = req | ||
| .max_cost_usd | ||
| .map(|cap| AgentBudget::builder(req.agent_id.clone(), cap).build()) | ||
| .transpose() | ||
| .map_err(|e| IIIError::Handler(e.to_string()))?; | ||
|
|
||
| let config: Option<AgentConfig> = iii | ||
| .trigger(TriggerRequest { | ||
|
|
@@ -229,7 +235,10 @@ async fn agent_chat(iii: &III, req: ChatRequest) -> Result<Value, IIIError> { | |
| let mut iterations: u32 = 0; | ||
|
|
||
| while let Some(tool_calls) = response.get("toolCalls").and_then(|v| v.as_array()) { | ||
| if tool_calls.is_empty() || iterations >= MAX_ITERATIONS { | ||
| if tool_calls.is_empty() | ||
| || iterations >= MAX_ITERATIONS | ||
| || over_budget(budget.as_ref(), &response).await | ||
| { | ||
| break; | ||
| } | ||
| iterations += 1; | ||
|
|
@@ -427,11 +436,46 @@ async fn list_functions(iii: &III, agent_id: &str) -> Result<Value, IIIError> { | |
| Ok(json!(filtered)) | ||
| } | ||
|
|
||
| /// Books one llm::complete response against the run's dollar cap. | ||
| /// True once the cap is spent or the model cannot be priced. | ||
| async fn over_budget(budget: Option<&AgentBudget>, response: &Value) -> bool { | ||
| let Some(budget) = budget else { return false }; | ||
| let model = response["model"].as_str().unwrap_or(""); | ||
| let input = response["usage"]["input"].as_u64().unwrap_or(0) as u32; | ||
| let output = response["usage"]["output"].as_u64().unwrap_or(0) as u32; | ||
| match budget.charge_model(model, input, output, None, None).await { | ||
| Ok(_) => false, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Stop when a successful charge exhausts the cap. In
|
||
| Err(e) => { | ||
| tracing::warn!(error = %e, "agent run stopped by dollar cap"); | ||
| true | ||
| } | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use types::{ModelConfig, Capabilities, Resources}; | ||
|
|
||
| #[tokio::test] | ||
| async fn test_dollar_cap_stops_loop() { | ||
| let response = json!({ | ||
| "model": "claude-sonnet-4-20250514", | ||
| "usage": { "input": 10_000, "output": 1_000, "total": 11_000 }, | ||
| }); | ||
| assert!(!over_budget(None, &response).await); | ||
|
|
||
| // $0.045 per turn against a $0.10 cap: two turns fit, the third stops the loop. | ||
| let budget = AgentBudget::builder("hand", 0.10).build().unwrap(); | ||
| assert!(!over_budget(Some(&budget), &response).await); | ||
| assert!(!over_budget(Some(&budget), &response).await); | ||
| assert!(over_budget(Some(&budget), &response).await); | ||
|
|
||
| let unpriced = json!({ "model": "no-such-model", "usage": { "input": 1, "output": 1 } }); | ||
| let budget = AgentBudget::builder("hand", 1.0).build().unwrap(); | ||
| assert!(over_budget(Some(&budget), &unpriced).await); | ||
| } | ||
|
|
||
| #[test] | ||
| fn test_max_iterations_constant() { | ||
| assert_eq!(MAX_ITERATIONS, 50); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -56,6 +56,8 @@ pub struct HandAgent { | |
| #[serde(default)] | ||
| pub max_iterations: Option<u64>, | ||
| #[serde(default)] | ||
| pub max_cost_usd: Option<f64>, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Reject non-finite caps before serialization. TOML permits Validate supplied caps when loading Based on learnings, validate Rust configuration values immediately after loading and return descriptive errors.
|
||
| #[serde(default)] | ||
| pub temperature: Option<f64>, | ||
| #[serde(default)] | ||
| pub system_prompt: String, | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Record a budget stop as a distinct run outcome, not
completed.When
over_budgetreturnstruefor a response with tool calls, the loop exits. The pending tool calls never run.agent_chatstill returnsOk, and it returns no stop reason.run_handthen recordsstatus: "completed"in both the activity log and the returned object, so the record shows success for work that stopped early.Return a distinct budget-stopped outcome from
agent_chat. Map that outcome inrun_handto a status other thancompleted. Tool effects from earlier iterations stay as they are. Do not retry the whole run automatically.