Welcome to the world of tools! This tutorial teaches you how to create agents that can use custom functions and built-in tools to perform specific tasks. This is where your agents become truly powerful and capable of real-world actions.
Sign in to save downloads to your library and vote.
Preview
π― Tutorial 3: Tool Using Agent
Welcome to the world of tools! This tutorial teaches you how to create agents that can use custom functions and built-in tools to perform specific tasks. This is where your agents become truly powerful and capable of real-world actions.
π― What You'll Learn
- Function Tools: Creating custom Python functions as agent tools
- Built-in Tools: Using OpenAI's pre-built capabilities
- Tool Integration: Adding tools to agents effectively
- Tool Execution: Understanding how agents decide when to use tools
π§ Core Concept: Tools in OpenAI Agents SDK
Tools are functions that your agent can call to perform specific tasks. Think of them as the agent's "hands" - they allow the agent to:
- Perform calculations and data processing
- Search the web and access real-time information
- Execute code and analyze data
- Call external APIs and services
- Access databases and file systems
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
β AGENT WITH TOOLS β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ€
β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β INPUT βββββΆβ AGENT βββββΆβ OUTPUT β β
β β "Calculate β β Reasoning β β "Using the β β
β β compound β β + Tool Use β β calculator β β
β β interest" β β β β tool: $..."β β
β βββββββββββββββ βββββββββββββββ βββββββββββββββ β
β β β
β βΌ β
β βββββββββββββββ β
β β TOOLS β β
β β βββββββββββ β β
β β βCalculatorβ β β
β β βββββββββββ€ β β
β β βWeb Searchβ β β
β β βββββββββββ€ β β
β β βFile I/O β β β
β β βββββββββββ β β
β βββββββββββββββ β
βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
π§ Types of Tools
1. Function Tools
Custom Python functions you create:
@function_tool
def calculate_compound_interest(principal: float, rate: float, time: int) -> float:
"""Calculate compound interest"""
return principal * (1 + rate) ** time
2. Built-in Tools
OpenAI provides powerful pre-built tools:
- WebSearchTool: Search the web for current information
- CodeInterpreterTool: Execute Python code safely
- FileSearchTool: Search through uploaded files
π Tutorial Overview
This tutorial includes three focused tool integration examples:
1. Function Tools (3_1_function_tools/)
- Custom Python functions as tools
@function_tooldecorator usage- Basic mathematical and utility functions
2. Built-in Tools (3_2_builtin_tools/)
- OpenAI's WebSearchTool integration
- CodeInterpreterTool for computations
- Pre-built tool capabilities
3. Agents as Tools (3_3_agents_as_tools/)
- Using agents as tools for orchestration
- Specialized agent coordination
- Advanced agent composition patterns
π Project Structure
3_tool_using_agent/
βββ README.md # This file - concept explanation
βββ requirements.txt # Dependencies
βββ 3_1_function_tools/ # Custom function tools
β βββ __init__.py
β βββ tools.py # Custom tool definitions
β βββ agent.py # Agent with function tools (25 lines)
βββ 3_2_builtin_tools/ # Built-in tools integration
β βββ __init__.py
β βββ agent.py # Agent with built-in tools (30 lines)
βββ 3_3_agents_as_tools/ # Agents as tools pattern
β βββ __init__.py
β βββ agent.py # Basic agent orchestration (40 lines)
β βββ advanced_agent.py # Custom agent tools with Runner config
βββ app.py # Streamlit web interface (optional)
βββ env.example # Environment variables template
π― Learning Objectives
By the end of this tutorial, you'll understand:
- β
How to create custom function tools with
@function_tool - β How to integrate built-in tools like WebSearch and CodeInterpreter
- β How agents decide when and how to use tools
- β Best practices for tool design and integration
- β Error handling and tool validation
π Getting Started
-
Install OpenAI Agents SDK:
pip install openai-agents -
Install dependencies:
pip install -r requirements.txt -
Set up environment variables:
cp env.example .env # Edit .env and add your OpenAI API key -
Test the calculator agent:
python calculator_agent.py -
Test the research agent:
python research_agent.py -
Test the data analysis agent:
python data_analysis_agent.py -
Run the interactive web interface:
streamlit run app.py
π§ͺ Sample Use Cases
Agents as Tools
Try these orchestration requests:
- "Translate 'Hello, how are you?' to Spanish and French"
- "Say 'Good morning' in all available languages"
- "Research artificial intelligence and write a professional summary"
Research Agent
Try these information requests:
- "What's the latest news about artificial intelligence?"
- "Find information about renewable energy trends in 2024"
- "Search for Python programming best practices"
Data Analysis Agent
Try these data requests:
- "Analyze this CSV data: [paste some data]"
- "Create a simple bar chart of sales data"
- "Calculate statistical measures for this dataset"
π§ Key Tool Patterns
1. Simple Function Tool
@function_tool
def add_numbers(a: float, b: float) -> float:
"""Add two numbers together"""
return a + b
2. Complex Function Tool with Validation
@function_tool
def get_weather(city: str, units: str = "metric") -> str:
"""Get current weather for a city"""
if not city.strip():
return "Error: City name cannot be empty"
# API call logic here
return f"Weather data for {city}"
3. Agents as Tools Integration
from agents import Agent
# Define specialized agents
translator = Agent(name="Translator", instructions="Translate text")
# Use agent as a tool
orchestrator = Agent(
name="Orchestrator",
instructions="Coordinate translation tasks",
tools=[
translator.as_tool(
tool_name="translate_text",
tool_description="Translate user's message"
)
]
)
π‘ Tool Design Best Practices
- Clear Docstrings: Tools need descriptive docstrings for the agent to understand their purpose
- Type Hints: Always use proper type hints for parameters and return values
- Error Handling: Handle errors gracefully and return meaningful messages
- Simple Parameters: Keep tool parameters simple and well-defined
- Single Purpose: Each tool should do one thing well
π Next Steps
After completing this tutorial, you'll be ready for:
- Tutorial 4: Runner Execution Methods - Master different execution patterns
- Tutorial 5: Context Management - Manage state across interactions
- Tutorial 6: Guardrails & Validation - Add safety and validation
π¨ Troubleshooting
- Tool Not Called: Check that your tool docstring clearly describes its purpose
- Type Errors: Verify that parameter types match the function signature
- Import Issues: Make sure you've imported the
function_tooldecorator - API Errors: For built-in tools, check your OpenAI API key and permissions
Ingestion metadata
- Source catalog
- awesome-llm-apps
- Repository
- Shubhamsaboo/awesome-llm-apps Β· main
- File path
- ai_agent_framework_crash_course/openai_sdk_crash_course/3_tool_using_agent/README.md
- Last refreshed
- 7/24/2026, 3:00:13 AM (53m ago)
- Refresh schedule
- Daily Β· 03:00 UTC
- Dedupe status
- Unique Β· deduped by (source, url)