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: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.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.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.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
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: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 operationsDeployment Checklist
Before deploying agentic features to production: Environment & Configuration Error Handling & Reliability Security Monitoring & Observability Testing Cost ManagementNext 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