Inkdown
Start writing

Study

70 filesยท12 subfolders

Shared Workspace

Study
AI eng

19_BEST_PRACTICES

Shared from "Study" on Inkdown

Best Practices - Comprehensive Deep Dive

Overview

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:

Python
basic-ques
core
Revision w/ Whiteboard
CN Basics - 1
CN Basics - 2
DNS
Event loop
programming-language-concepts.md
zero-language-explanation.md
DB
Quick
databases-deep-dive.md
01-introduction.md
02-relational-databases.md
03-database-design.md
04-indexing.md
05-transactions-acid.md
06-nosql-databases.md
07-query-optimization.md
08-replication-ha.md
09-sharding-partitioning.md
10-caching-strategies.md
11-cap-theorem.md
12-connection-pooling.md
13-backup-recovery.md
14-monitoring.md
15-database-selection.md
README.md
JS
core topics
Event loop
Merlin Backend
01-Orchestration.md
02-DeepResearch.md
03-Search.md
04-Scraping.md
05-Streaming.md
06-MultiProviderLLM.md
07-MemoryAndContext.md
08-ErrorHandling.md
09-RateLimiting.md
10-TaskQueue.md
11-SecurityAndAuth.md
Orchestration-2nd-draft
Mobile
Build Alternative
Bundling
metro-bundler-deep-dive.md
OpenAI Agents Python
00_OVERVIEW.md
01_AGENT_SYSTEM.md
02_RUNNER_SYSTEM.md
03_TOOL_SYSTEM.md
04_ITEMS_SYSTEM.md
05_GUARDRAILS.md
06_HANDOFFS.md
07_MEMORY_SESSIONS.md
08_MODEL_PROVIDERS.md
09_SANDBOX_SYSTEM.md
10_TRACING.md
11_RUN_STATE.md
12_CONTEXT.md
13_LIFECYCLE_HOOKS.md
14_CONFIGURATION.md
15_ERROR_HANDLING.md
16_STREAMING.md
17_EXTENSIONS.md
18_MCP_INTEGRATION.md
19_BEST_PRACTICES.md
20_ARCHITECTURE_PATTERNS.md
opencode-study
context-handling
core
Python
Alembic
Basics
sqlalchemy - fastapi
SQLAlchemy overview
tweets
system_design_for_agentic_apps.md
Agent Loop
2. Iterate Quickly

Build, test, and iterate quickly:

Python
3. Use Type Hints

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:

  1. Build robust agents that handle errors gracefully
  2. Maintain code that is easy to understand and modify
  3. Scale applications that perform well under load
  4. Secure systems that protect sensitive data
  5. Monitor applications with good observability
  6. Test code that catches bugs early
  7. Document code that others can understand
  8. 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.