OpenAI AI Agents SDK

Discover how to create AI Agents using the OpenAI SDK

Cobus Greyling
5 min read4 days ago

--

Some Background

Below I do a walkthrough to show step-by-step how to build AI Agents by making use of the OpenAI AI Agents SDK. The SDK runs within a virtual environment on your machine and the Python code is executed from there.

The steps of the AI Agent are logged to the Dashboard which is shown below, for full inspectability.

What I like about this framework is how simple it is, with a good breakdown of primitives, model orchestration and tool use. With the telemetry which is just works out of the box, the framework makes for a great start.

Everything runs locally while accessing the Language models in the cloud, together with the telemetry.

One should however consider that when scaling, a more visual builder or no-code primitives will become a requirement. Here are some considerations when scaling in complex environments…

State Management Overhead

The SDK lacks native persistent state/flow management, requiring developers to implement custom solutions for agent memory and context in long-running or multi-agent workflows. This can add complexity but allows tailored control.

Tool Integration Challenges

The SDK supports custom tools via function_tool with schema generation, but orchestrating multiple external APIs can be complex.

Scaling workflows with many tools requires careful schema management and robust error handling.

Model-Driven Variability

The SDK’s performance hinges on the language model’s ability to interpret prompts and select tools.

Ambiguous or niche inputs may cause inconsistent results, a common challenge for LLM-based systems.

Resource Demands

Running multiple AKI Agents with frequent tool calls or handoffs can be resource-intensive.

The SDK lacks built-in optimisation for large-scale concurrent tasks, potentially increasing costs or latency without custom tuning.

Limited Orchestration

The SDK enables basic handoffs but lacks advanced features for dynamic multi-agent orchestration, such as real-time collaboration or failover. Complex workflows may need custom solutions.

AI Agents are intelligent systems designed to tackle tasks, from straightforward workflows to intricate, open-ended goals.

With the OpenAI API, you have access to a contained, simplified and versatile toolkit of composable primitives to create AI Agents.

Overview

Creating AI Agents means integrating components from various domains — like:

  • Models,
  • Tools,
  • Knowledge and
  • Memory,
  • Audio and
  • Speech,
  • Guardrails, and
  • Orchestration.

OpenAI attempts to offer flexible primitives for each, empowering you to build robust solutions.

If you like this article & want to show some love ❤️

- Clap 50 times, each one helps more than you think! 👏

- Follow me on Medium and subscribe for free. 🫶

- Also follow me on LinkedIn or on X! 🙂

Tools empower AI Agents to engage with their environment. With the OpenAI API, you can leverage function calling to integrate with your custom code, plus built-in tools for everyday tasks like web searches and data retrieval.

To run an OpenAI Agent environment locally on your machine, the best starting point is the Terminal application if you are using MacOs.

Create a virtual environment with the command:

python -m venv open_agent

In this example I called my virtual environment openai.

Then you can activate the virtual environment:

source openai/bin/activate

and also export your OpenAI API key for our AI Agent to make use of, all from the command line.

export OPENAI_API_KEY=<Your API Key>

Here are three simple demo application you can run from the command line with python file_name.py .

from agents import Agent, Runner

agent = Agent(name="Assistant", instructions="You are a helpful assistant")

result = Runner.run_sync(agent, "Write a haiku about recursion in programming.")
print(result.final_output)

The code sets up a simple AI Agent designed to act as a helpful assistant and tasks it with generating a haiku about recursion in programming.

The Runner.run_sync method processes the request, interfacing with a language model or similar system under the hood, and returns the generated haiku, which is then printed.

from agents import Agent, Runner
import asyncio

spanish_agent = Agent(
name="Spanish agent",
instructions="You only speak Spanish.",
)

english_agent = Agent(
name="English agent",
instructions="You only speak English",
)

triage_agent = Agent(
name="Triage agent",
instructions="Handoff to the appropriate agent based on the language of the request.",
handoffs=[spanish_agent, english_agent],
)


async def main():
result = await Runner.run(triage_agent, input="Hola, ¿cómo estás?")
print(result.final_output)
# ¡Hola! Estoy bien, gracias por preguntar. ¿Y tú, cómo estás?


if __name__ == "__main__":
asyncio.run(main())

This code sets up a system of AI Agents to handle requests in different languages using an asynchronous framework.

It defines three AI Agents:

  1. a Spanish agent that only responds in Spanish,
  2. an English agent that only responds in English, and a
  3. Triage agent that directs incoming requests to the appropriate AI Agent based on the language of the input.

The Triage agent is configured with a list of AI Agents it can hand off to (spanish_agent and english_agent).

import asyncio

from agents import Agent, Runner, function_tool


@function_tool
def get_weather(city: str) -> str:
return f"The weather in {city} is sunny."


agent = Agent(
name="Hello world",
instructions="You are a helpful agent.",
tools=[get_weather],
)


async def main():
result = await Runner.run(agent, input="What's the weather in Tokyo?")
print(result.final_output)
# The weather in Tokyo is sunny.


if __name__ == "__main__":
asyncio.run(main())

The code creates an AI Agent capable of answering questions by leveraging custom tools.

The AI Agent is equipped with a get_weather tool, which it uses to respond to the prompt “What’s the weather in Tokyo?”.

The AI Agent framework (via Runner) processes the input, determines that the get_weather tool is relevant, calls it with city=”Tokyo”, and returns the tool’s output (“The weather in Tokyo is sunny.”).

The framework is designed for scalable operations, to handle multiple requests or integrate with external APIs efficiently.

Lastly, AI Agent traces are accessible from the OpenAI dashboard as seen below…

--

--

Cobus Greyling
Cobus Greyling

Written by Cobus Greyling

I’m passionate about exploring the intersection of AI & language. www.cobusgreyling.com

No responses yet