Skip to main content

Overview

klaw provides several mechanisms to control costs, enforce safety boundaries, and maintain reliability. This guide covers setting up cost budgets, tool approval gates, provider resilience, monitoring, and per-agent restrictions.

Cost Budgets

Setting a Session Budget

Add max_session_cost to your defaults configuration to cap spending per session:
When the accumulated cost reaches this limit, the agent stops with a budget_exceeded error. A warning is logged when cost reaches 80% of the budget.

How Cost is Calculated

klaw tracks input and output tokens for every LLM call and multiplies by per-model pricing:
Built-in pricing for common models: Unknown models default to 3.00input/3.00 input / 15.00 output per million tokens.

Tips for Cost Control

Begin with max_session_cost = 2.00 for interactive sessions and increase as needed. For automated pipelines, set higher limits but always have a cap.
Combine cost budgets with max_iterations to prevent runaway loops:
Use Haiku (0.80/1Minput)forsimpletaskslikerouting,Sonnet(0.80/1M input) for simple tasks like routing, Sonnet (3/1M) for most work, and Opus ($15/1M) only for complex reasoning.

Tool Approval

Configuring Approval Gates

Require user confirmation before specific tools execute:
When the agent tries to use an approved tool, the user sees:
  • Type y or yes to approve
  • Any other response (or Enter) denies execution
  • Denied tools return “Denied by user” to the LLM, which can adjust its approach

Which Tools to Gate

Provider Resilience

Retry Configuration

Each provider retries failed requests with exponential backoff:
Retry behavior:
  • Backoff: Starts at 1s, doubles each attempt, caps at 30s
  • Jitter: Random 0-25% added to prevent thundering herd
  • Retryable errors: HTTP 429 (rate limit), 500, 502, 503, timeouts, connection resets

Fallback Providers

Chain providers so failures on one automatically route to another:
The flow is: try primary with retries → exhaust retries → try fallback with its own retries.

Monitoring

Structured Logging

Enable JSON-formatted logs for production monitoring:
Log entries include structured fields:

Metrics

klaw tracks metrics globally and per-session:

Per-Agent Restrictions

Tool Filtering

Restrict which tools an agent can access:
When tools is omitted, the agent has access to all registered tools. When specified, only listed tools are available.

Combining Safety Features

For maximum control, combine multiple safety mechanisms:
This configuration:
  • Caps session cost at $10
  • Limits the default agent to 50 iterations with bash approval
  • Gives the researcher agent only read-only tools with 30 iterations
  • Retries Anthropic API calls 3 times then falls back to OpenRouter

Next Steps

Configuration

Full configuration reference

Resilience Architecture

Deep dive into retry, fallback, and error handling