Skip to main content

Using OpenAI's Agent + Runner Interface

In this lesson, you’ll learn how to use the new Agent and Runner abstraction from OpenAI’s experimental agent framework to create assistants that can follow instructions and reason over input.

Prerequisites

Install the latest openai Python SDK (with agent support):

pip install --upgrade openai

The Code

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)

Explanation

  • Agent(...): Defines the persona and behavior for your assistant.
  • Runner.run_sync(...): Executes the agent synchronously with a provided input.
  • result.final_output: Contains the agent’s completed response.

Use Case

Best for:

  • Lightweight assistant prototypes
  • Single-turn queries with context and role definition
  • Experimental agent workflows with zero setup