Skip to main content

Type Safety

Strong type hints do two things: they guide agents toward the correct output structure, and they give you type-safe returns in your code. The more specific your types, the more constrained an agent’s output will be. Use literal types to restrict outputs to specific values:
Use structured types for complex outputs. The agent will match your type structure exactly:
Combine types with validation for even stronger guarantees. See Error Handling for validation patterns using Pydantic and Zod.

Security

Credential Management

Never hardcode API keys or secrets. Use environment variables. This keeps credentials out of your codebase and allows different values per environment.
Never pass raw API keys to agents. Instead, pass pre-authenticated SDK clients or specific methods. The agent uses the functionality without ever seeing the credentials:

Input Validation

Validate user input before passing it to agentic functions. This prevents injection attacks and ensures your agentic functions receive clean data.

File Access Scope

Agents that can open arbitrary paths can easily escape their intended sandbox (for example by traversing ../) and read, modify, or delete files across your system. Avoid passing Path objects or unrestricted file paths directly to agents or agentic functions. Instead, pre-open only the specific files you want the agent to access and pass those file handles in scope.

Rate Limiting

Implement rate limiting to protect against abuse and manage costs. This is especially important for user-facing features.
Exponential backoff handles transient failures when agentic functions or agents call external APIs that may be rate-limited.

Monitoring

Track these key metrics in production to understand your agentic operations:
  • Latency. How long do agentic functions and agents take to respond?
  • Error rates. What percentage of agentic calls fail or timeout?
  • Usage patterns. Which functions are called most? By which users?
  • Output quality. Are results meeting expectations? Use sampling to review outputs.

Logging

Log agentic operations with structured data. Include the operation name, input size, model used, and timing. This helps debug issues and identify patterns.
Never log sensitive data. User inputs, API keys, or PII should not appear in logs. See Error Handling › Sensitive Data Handling for examples of safe logging practices.

Performance

Caching

Cache agent responses when the same inputs produce the same outputs. This reduces latency and costs for repeated operations. Use caching for:
  • Reference data that changes infrequently (product descriptions, documentation)
  • Expensive operations called repeatedly with the same inputs
  • Read-heavy workflows where consistency is acceptable
Advanced: Best-of-N caching with retries. Like JIT compilation that eventually compiles hot code paths, you can combine caching with retry strategies to create a “best-of-N” pattern: retry failed operations until you get a high-quality result, then cache that successful response. Future calls skip the retry logic entirely and use the cached “compiled” result. This is particularly useful for expensive operations where you want to pay the retry cost once, then reuse the validated output.

Parallel Processing

Process multiple items in parallel when they’re independent. This is faster than sequential processing.

Stateful Workflows with Agents

Use agents for multi-step workflows where later steps depend on earlier results. Agents maintain context across invocations, allowing them to make decisions based on what they’ve already done. Here’s an agent that debugs code by analyzing, then deciding whether to fix or explain based on what it finds:
For truly independent operations, use agentic functions and process in parallel. For dependent workflows where context matters, use a single agent across multiple calls.

Cost Optimization

Inference costs money — optimize by choosing the right model, caching responses, and using agents only when needed. Choose the right model for the task. Use cheaper models for simple operations, more expensive models for complex reasoning. See Model Selection for guidance. Cache aggressively. Every cache hit is a cost you don’t pay. See Caching above. Keep prompts concise. Longer prompts cost more. Remove unnecessary context or examples once you’ve validated your agentic function works. Use agents strategically. Agents maintain conversation history, which grows with each call and costs more. For stateless operations, use agentic functions instead. Bad: Using an agent for independent operations
Good: Using agentic function for independent operations
Good: Using agent when context matters

Deployment Checklist

Before deploying agentic features to production: Environment & Configuration Error Handling & Reliability Security Monitoring & Observability Testing Cost Management

Next Steps

Human-in-the-Loop

Add human oversight to your agents

Examples

See production-ready examples

Advanced

Custom system prompts and templating

API Reference

Complete API documentation