Skip to main content

What this snippet demonstrates

Runaway agent loops are the most common source of unexpected API costs. AFK provides two defense layers: cost budgets that kill runs when spending exceeds a threshold, and telemetry events that let you observe cost in real time. This snippet shows how to configure both.

Setting cost budgets

The simplest defense is a hard cost ceiling on every agent:
When the estimated cost exceeds max_total_cost_usd, the runner terminates the run with a degraded state and returns the best partial result.

Monitoring cost from results

Every AgentResult includes token counts and cost estimates:

Real-time cost monitoring via streaming

For long-running agents, monitor cost during execution:

Cost-aware batch processing

When running multiple agents in a batch, track cumulative cost:

Operating recommendations

  1. Always set max_total_cost_usd — even generous limits prevent runaway costs
  2. Layer defenses — combine cost limits with max_llm_calls, max_steps, and max_wall_time_s
  3. Use telemetry for dashboards — export metrics to monitor cost trends over time
  4. Set per-item budgets in batches — prevent one expensive item from consuming the entire budget
  5. Choose models by task — use smaller models for routine work and reserve larger models for requests that need them