Agents SDK: Installation and Your First Agent
On this page
Agents SDK: Installation and Your First Agent
Let’s get coding. You will install OpenAI’s official Agents SDK, build a working Python agent, and explore the three execution modes you’ll use throughout this course.
Prerequisites
You need just three things: Python 3.10+ installed on your machine, an OpenAI account with an active API key, and a code editor. We recommend VS Code, but any editor with Python syntax highlighting works fine.
Installation
The package is named openai-agents. Watch out: installing openai alone gives you the classic API client, not the Agents SDK.
pip install openai-agents
This package automatically installs required dependencies, including the OpenAI Python SDK. Next, set your API key as an environment variable. The SDK reads it automatically, so no extra configuration is needed.
export OPENAI_API_KEY="sk-proj-your-api-key"
This command disappears when you close the terminal, and you certainly don’t want to hard-code your key in your source files. For a proper project setup, use a .env file with python-dotenv, and add that file to your .gitignore before your first commit.
pip install python-dotenv
# .env
OPENAI_API_KEY=sk-proj-your-api-key
Your First Agent
Create a file named my_first_agent.py:
from agents import Agent, Runner
agent = Agent(
name="Assistant",
instructions="You are a helpful assistant. Respond in English concisely.",
model="gpt-5.6-terra",
)
result = Runner.run_sync(agent, "Explain what an AI agent is in three sentences.")
print(result.final_output)
Run it:
python my_first_agent.py
You just created and ran your first agent. It’s simple—no tools, no guardrails—but it forms the foundation for everything else. The rest of this course simply builds on these two objects: Agent and Runner.
Asynchronous Execution
run_sync blocks the current thread until the response arrives. That’s fine for a test script, but unacceptable for a web server handling hundreds of concurrent requests. In production, you’ll use asynchronous mode.
import asyncio
from agents import Agent, Runner
agent = Agent(
name="Async Assistant",
instructions="You are a helpful assistant. Respond in English.",
model="gpt-5.6-terra",
)
async def main():
result = await Runner.run(agent, "What are the advantages of Python for AI?")
print(result.final_output)
asyncio.run(main())
Runner.run() is the asynchronous version; Runner.run_sync() is a synchronous wrapper that calls it for you. When using FastAPI or any other async framework, always use Runner.run().
Streaming Execution
There’s a third mode, essential whenever a human is waiting at the screen. Streaming displays the response as it’s generated, token by token.
import asyncio
from agents import Agent, Runner
agent = Agent(
name="Streaming Assistant",
instructions="You are a helpful assistant. Respond in English.",
model="gpt-5.6-terra",
)
async def main():
result = Runner.run_streamed(agent, "Tell a short story about AI.")
async for event in result.stream_events():
if event.type == "raw_response_event":
if hasattr(event.data, "delta") and event.data.delta:
print(event.data.delta, end="", flush=True)
print() # New line at the end
asyncio.run(main())
The async for loop consumes a stream of events. Here, we filter for raw model response events and print only the text fragments. Streaming is critical for user experience: nobody wants to stare at a blank screen for 10 seconds.
Agent Project Structure
A production agent doesn’t fit in a single file. Separate your agent definitions, tools, and tests from the start, or your main.py will quickly become unreadable.
my-agent-project/
├── .env # API key
├── requirements.txt # Dependencies
├── agents/
│ ├── __init__.py
│ ├── assistant.py # Agent definition
│ └── tools.py # Tool definitions
├── main.py # Entry point
└── tests/
└── test_agent.py # Tests
# requirements.txt
openai-agents>=0.1.0
python-dotenv>=1.0.0
Key Takeaways
- Install the SDK with
pip install openai-agents - Set
OPENAI_API_KEYas an environment variable - Use
Runner.run_sync()for development andRunner.run()for async production - Use
Runner.run_streamed()for real-time streaming - Structure your project with separate folders for agents, tools, and tests