Skip to main content
Moving from development to production means handling the things that don’t matter during prototyping: errors, costs, observability, and security. This guide covers the patterns that production agent deployments need.

Error Handling

Agent-Level Errors

The direct AgentRuntime result reports status; the host-facing AgentResult used by core reports finishReason.

Tool Errors

Tools should return errors as data rather than throwing. This lets the agent see the error and adjust:

Mistake Limits

Core sessions can track consecutive recoverable mistakes and stop with finishReason: "mistake_limit". Direct runtime runs report failure through status and error.

Loop Detection

Loop detection is available through core/session execution settings.

Observability

OpenTelemetry Integration

The SDK integrates with OpenTelemetry for traces, metrics, and logs:
Events emitted include:
  • agent_created — agent initialized with config
  • tool_usage — tool called with name, duration, success/failure
  • model_api_call — LLM API call with model, tokens, latency
  • session_ended — session completed with finish reason, total tokens

Custom Metrics via Plugins

Structured Logging

Cost Control

Set Token Limits

Track Spending

Use Cheaper Models for Simple Tasks

Security

Sandbox Tool Execution

Never let an agent run arbitrary shell commands on a production server without constraints:

Validate Tool Inputs

Don’t trust tool inputs any more than you’d trust user input:

Rotate API Keys

Use environment variables for API keys, not hardcoded values:

Deployment Patterns

Stateless Worker

For request/response workflows (API endpoints, webhook handlers):

Persistent Service

For long-running services with session management: