This document compiles the best practices for using the OpenAI Agents Python SDK. These practices are distilled from real-world usage, community feedback, and the core development team's experience. Following these practices will help you build robust, maintainable, and efficient agent-based applications.
General Principles
1. Start Simple
Begin with simple agents and add complexity gradually:
Always use type hints for better IDE support and type safety:
Python
4. Write Clear Instructions
Write clear, specific agent instructions:
Python
5. Test Thoroughly
Test your agents extensively:
Python
Agent Design
1. Single Responsibility
Each agent should have a single, clear responsibility:
Python
2. Clear Naming
Use descriptive agent names:
Python
3. Appropriate Instructions
Match instructions to the agent's purpose:
Python
4. Tool Selection
Only include relevant tools:
Python
5. Handoff Descriptions
Write clear handoff descriptions:
Python
Tool Design
1. Clear Names
Use descriptive tool names:
Python
2. Type Hints
Always use type hints:
Python
3. Docstrings
Write clear docstrings:
Python
4. Error Handling
Handle errors gracefully:
Python
5. Input Validation
Validate inputs:
Python
Guardrails
1. Use Appropriate Guardrails
Use guardrails at the right level:
Python
2. Clear Tripwire Conditions
Make it clear when guardrails trigger:
Python
3. Descriptive Output Info
Provide helpful output info:
Python
4. Parallel When Possible
Use parallel execution for independent checks:
Python
5. Handle Errors Gracefully
Guardrails should handle their own errors:
Python
Configuration
1. Environment-Based Configuration
Use different configs for different environments:
Python
2. Configuration Files
Store configuration in files:
Python
3. Sensitive Data in Environment
Never hardcode sensitive data:
Python
4. Validate Configuration
Validate configuration early:
Python
5. Document Configuration
Document configuration options:
Python
Context
1. Use Dataclasses
Use dataclasses for context:
Python
2. Keep Context Focused
Keep context focused on relevant data:
Python
3. Use Type Hints
Always use type hints:
Python
4. Document Context Fields
Document context fields:
Python
5. Avoid Circular Dependencies
Avoid circular references in context:
Python
Error Handling
1. Catch Specific Exceptions
Catch specific exceptions:
Python
2. Provide Context in Errors
Include helpful context:
Python
3. Log Before Raising
Log errors before re-raising:
Python
4. Use Custom Error Types
Define custom error types:
Python
5. Handle Errors Gracefully
Provide graceful fallbacks:
Python
Performance
1. Use Async
Always use async for production:
Python
2. Set Reasonable Limits
Set reasonable limits:
Python
3. Use Appropriate Models
Choose the right model for the task:
Python
4. Enable Tracing Selectively
Enable tracing when needed:
Python
5. Use Sessions for Long Conversations
Use sessions to manage conversation history:
Python
Security
1. Validate Inputs
Validate all inputs:
Python
2. Use Guardrails
Use guardrails for safety:
Python
3. Require Approval for Sensitive Tools
Require approval for sensitive operations:
Python
4. Exclude Sensitive Data from Traces
Exclude sensitive data from traces:
Python
5. Use Environment Variables
Store secrets in environment variables:
Python
Testing
1. Test All Code Paths
Test all scenarios:
Python
2. Use Fixtures
Use fixtures for common setup:
Python
3. Mock External Dependencies
Mock external services:
Python
4. Test Edge Cases
Test edge cases:
Python
5. Test Configuration
Test with different configurations:
Python
Documentation
1. Document Agent Purpose
Document what each agent does:
Python
2. Document Tools
Document tool behavior:
Python
3. Document Configuration
Document configuration options:
Python
4. Document Extensions
Document extension behavior:
Python
5. Document API
Document public API:
Python
Monitoring
1. Track Metrics
Track key metrics:
Python
2. Log Important Events
Log important events:
Python
3. Monitor Performance
Monitor performance metrics:
Python
4. Alert on Errors
Alert on critical errors:
Python
5. Use Tracing
Use tracing for observability:
Python
Deployment
1. Use Environment Variables
Use environment variables for configuration:
Python
2. Graceful Shutdown
Handle graceful shutdown:
Python
3. Health Checks
Implement health checks:
Python
4. Resource Limits
Set resource limits:
Python
5. Rate Limiting
Implement rate limiting:
Python
Summary
Following these best practices will help you:
Build robust agents that handle errors gracefully
Maintain code that is easy to understand and modify
Scale applications that perform well under load
Secure systems that protect sensitive data
Monitor applications with good observability
Test code that catches bugs early
Document code that others can understand
Deploy applications reliably
Remember that best practices evolve with experience. Start with these guidelines, learn from your own experience, and adapt them to your specific use cases.