OpenAI Agents SDK Cheat Sheet¶
1. Introduction to OpenAI Agents SDK¶
What is the OpenAI Agents SDK?¶
The OpenAI Agents SDK is an open-source Python library designed to simplify the development of agentic applications powered by OpenAI's LLMs. It offers a modular, composable set of tools and primitives to create deterministic flows, iterative loops, and multi-agent systems with minimal abstractions.
Key Features and Benefits¶
- Lightweight: Minimal overhead for rapid development.
- Flexible: Supports custom workflows, tools, and agent handoffs.
- Powerful: Leverages OpenAI's state-of-the-art LLMs.
- Structured Outputs: Integrates with Pydantic for typed responses.
- Metrics Tracking: Built-in usage monitoring for optimization.
2. Core Components¶
Agents¶
Agents are the central entities in the SDK, representing an LLM configured with:
- Name: A unique identifier.
- Instructions: A string defining the agent's behavior and purpose.
- Model: The underlying LLM (e.g., gpt-4o
, gpt-3.5-turbo
).
- Tools: Functions or services the agent can utilize.
Tools¶
Tools extend agent capabilities by allowing them to interact with external systems or perform specific tasks:
- Custom Tools: Defined using the @function_tool
decorator.
- Hosted Tools: Pre-built tools like FileSearchTool
, WebSearchTool
, and ComputerTool
.
Runners¶
Runners manage the execution of agents, handling input processing and output generation: - Asynchronous: For non-blocking execution. - Synchronous: For simpler, blocking workflows. - Streaming: For real-time event handling.
Guardrails¶
Guardrails ensure data integrity and enforce constraints:
- Defined with the @guardrail
decorator.
- Validate inputs and outputs (e.g., length, format, content).
Handoffs¶
Handoffs enable multi-agent collaboration by allowing one agent to delegate tasks to another, creating complex workflows.
3. Setting Up the SDK¶
Prerequisites¶
- Python 3.8+
- OpenAI API key (available from platform.openai.com)
Installation¶
Install the SDK via pip:
Bash | |
---|---|
Configuration¶
Set your API key in the environment:
Optionally, configure a custom API base URL:
Python | |
---|---|
4. Building Agents¶
Defining an Agent¶
Create an agent with a name, instructions, model, and optional tools:
Python | |
---|---|
Configuring Instructions¶
Instructions should be clear and specific to shape the agent's behavior:
Python | |
---|---|
Selecting Models¶
Supported models include:
- gpt-4o
: Latest high-performance model.
- gpt-3.5-turbo
: Cost-effective and fast.
- Custom fine-tuned models (if available).
Adding Tools¶
Attach tools to an agent for enhanced functionality:
Python | |
---|---|
5. Tools and Decorators¶
@function_tool¶
Convert a Python function into an agent-callable tool:
Python | |
---|---|
- Automatically generates a JSON schema for parameters.
- Supports type hints for validation.
@guardrail¶
Define validation logic for inputs or outputs:
Python | |
---|---|
- Returns
True
if valid,False
otherwise. - Can be applied to tools or standalone.
Hosted Tools¶
Pre-built tools provided by the SDK:
- FileSearchTool
: Search within files.
- WebSearchTool
: Query the web.
- ComputerTool
: Execute OS-level commands.
Example usage:
Python | |
---|---|
6. Running Agents¶
Asynchronous Execution¶
Run an agent asynchronously for non-blocking workflows:
Python | |
---|---|
Synchronous Execution¶
Run an agent synchronously for simpler use cases:
Streaming Results¶
Stream agent responses in real-time:
Python | |
---|---|
- Events include intermediate outputs, tool calls, and final results.
7. Multi-Agent Workflows¶
Handoffs¶
Delegate tasks between agents:
Agents as Tools¶
Use an agent as a callable tool:
Python | |
---|---|
8. Structured Outputs¶
Using Pydantic Models¶
Define structured responses with Pydantic:
- Ensures type safety and consistent output formats.
9. Usage Metrics¶
Tracking Requests and Tokens¶
Monitor API usage via the RunResult
object:
Python | |
---|---|
- Useful for cost estimation and performance optimization.
10. Advanced Features¶
Customizing Agent Behavior¶
Adjust agent settings: - Temperature: Controls randomness (0.0–2.0, default 1.0). - Max Tokens: Limits response length.
Python | |
---|---|
Error Handling¶
Handle exceptions gracefully:
Python | |
---|---|
Debugging¶
Enable debug mode for detailed logs:
Python | |
---|---|
11. Best Practices¶
- Clear Instructions: Use precise language to define agent roles.
- Modular Design: Create reusable tools and agents.
- Validation: Apply guardrails to enforce constraints.
- Optimization: Monitor usage metrics to reduce costs.
- Testing: Simulate edge cases to ensure robustness.
- Version Control: Track changes to agent configurations.
12. Additional Resources¶
- Official Documentation: Full SDK reference.
- OpenAI Platform Docs: API details and model info.
- Building Agents Guide: Step-by-step tutorials.
- GitHub Repository: Source code and issues.
- YouTube Tutorials: Video guides.
Happy coding Guys!