
Abid Ali Awan
· 2 min read
Building Your First AI Agent in Python with the OpenAI Agents SDK
Building an AI agent in Python is now much simpler than it used to be. With the OpenAI Agents SDK, you can create agents, connect tools, maintain conversation state, and run multi-step workflows without building your own tool-calling loop from scratch.
In this tutorial, we will build a practical research agent that can search the web, call a custom Python function, remember previous messages, and decide when to use each capability.
What Is an AI Agent?
A standard large language model (LLM) application usually follows a simple flow:
Prompt — Model — Response
The model receives a prompt, generates an answer, and stops.
An AI agent adds tools and an execution loop:
User — Agent — Tool if needed — Agent — Final Response
Instead of only generating text, the agent can decide whether it needs to take an action first, such as searching the web, calling a Python function, or retrieving information from memory.
With the OpenAI Agents SDK, the Runner manages this process automatically. It sends the request to the model, executes any requested tools, passes the results back to the agent, and continues until the agent produces a final answer.
This ability to reason, use tools, and continue working across multiple steps is what makes an agent different from a basic LLM application.
Set Up the Project
Before we build the agent, let's create a clean Python environment and install everything we need.
Create a Python project with uv:
mkdir first-ai-agent
cd first-ai-agent
uv init
uv add openai-agents
Or install the package with pip:
pip install openai-agents
Set your OpenAI API key:
export OPENAI_API_KEY="your-api-key"
On Windows, export works in Git Bash. In Command Prompt or PowerShell, you can use:
setx OPENAI_API_KEY "your-api-key"
Now we can turn a few lines of Python into an agent that decides when it needs fresh information from the web.
Create main.py:
Run it:
uv run main.py
Output:
For example, our run correctly identified Python 3.14.7 as the current stable release.
Add Your Own Python Tool
Test it:
Original source
This story was published by KDnuggets and written by Abid Ali Awan. SyncAI.news shows a preview; the complete article is on the publisher's site.
Read the full story on kdnuggets.com


