Code beginner · 3 min read

How to build an AI agent in python with OpenAI

Direct answer
Use the OpenAI Python SDK OpenAI client to create a chat completion with a system prompt defining the agent's role and user messages to interact, enabling a basic AI agent in Python.

Setup

Install
bash
pip install openai
Env vars
OPENAI_API_KEY
Imports
python
import os
from openai import OpenAI

Examples

inUser: What's the weather like today?
outAgent: I don't have real-time weather data, but you can check a weather website or app for the latest updates.
inUser: Write a Python function to reverse a string.
outAgent: Here's a Python function to reverse a string: ```python def reverse_string(s): return s[::-1] ```
inUser: Summarize the plot of 'The Great Gatsby'.
outAgent: 'The Great Gatsby' is about Jay Gatsby's pursuit of wealth and love in 1920s America, exploring themes of decadence and the American Dream.

Integration steps

  1. Install the OpenAI Python SDK and set your API key in the environment variable OPENAI_API_KEY
  2. Import the OpenAI client and initialize it with the API key from os.environ
  3. Define the agent's behavior with a system message describing its role
  4. Build a messages list including the system message and user input
  5. Call the chat.completions.create method with the chosen model and messages
  6. Extract and display the agent's response from the API output

Full code

python
import os
from openai import OpenAI

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Define a simple AI agent function

def ai_agent(user_input: str) -> str:
    messages = [
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": user_input}
    ]
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=messages
    )
    return response.choices[0].message.content

# Example usage
if __name__ == "__main__":
    user_question = "Write a Python function to check if a number is prime."
    answer = ai_agent(user_question)
    print("Agent:", answer)
output
Agent: Here's a Python function to check if a number is prime:

```python
def is_prime(n):
    if n <= 1:
        return False
    for i in range(2, int(n ** 0.5) + 1):
        if n % i == 0:
            return False
    return True
```

API trace

Request
json
{"model": "gpt-4o", "messages": [{"role": "system", "content": "You are a helpful AI assistant."}, {"role": "user", "content": "Write a Python function to check if a number is prime."}]}
Response
json
{"choices": [{"message": {"content": "Here's a Python function to check if a number is prime: ..."}}], "usage": {"total_tokens": 120}}
Extractresponse.choices[0].message.content

Variants

Streaming response version

Use streaming to display the agent's response token-by-token for better user experience with long outputs.

python
import os
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

messages = [
    {"role": "system", "content": "You are a helpful AI assistant."},
    {"role": "user", "content": "Explain recursion with an example."}
]

response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages,
    stream=True
)

for chunk in response:
    print(chunk.choices[0].delta.get('content', ''), end='')
Async version

Use async when integrating the agent into applications requiring concurrency or non-blocking calls.

python
import os
import asyncio
from openai import OpenAI

client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

async def ai_agent_async(user_input: str) -> str:
    messages = [
        {"role": "system", "content": "You are a helpful AI assistant."},
        {"role": "user", "content": user_input}
    ]
    response = await client.chat.completions.acreate(
        model="gpt-4o",
        messages=messages
    )
    return response.choices[0].message.content

async def main():
    answer = await ai_agent_async("Explain polymorphism in OOP.")
    print("Agent:", answer)

if __name__ == "__main__":
    asyncio.run(main())
Alternative model: Claude 3.5 Sonnet

Use Claude 3.5 Sonnet for stronger coding capabilities or when you prefer Anthropic's API.

python
import os
import anthropic

client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])

system_prompt = "You are a helpful AI assistant."
user_input = "Generate a JavaScript function to debounce a button click."

response = client.messages.create(
    model="claude-3-5-sonnet-20241022",
    max_tokens=512,
    system=system_prompt,
    messages=[{"role": "user", "content": user_input}]
)

print("Agent:", response.content[0].text)

Performance

Latency~800ms for gpt-4o non-streaming calls
Cost~$0.002 per 500 tokens for gpt-4o
Rate limitsTier 1: 500 requests per minute (RPM) / 30,000 tokens per minute (TPM)
  • Keep system prompts concise to reduce token usage
  • Batch user inputs when possible to minimize calls
  • Use shorter model variants like gpt-4o-mini for cost savings on simpler tasks
ApproachLatencyCost/callBest for
Standard call (gpt-4o)~800ms~$0.002General purpose AI agents
Streaming (gpt-4o)Starts ~300ms, streams tokens~$0.002Long responses, better UX
Async (gpt-4o)~800ms~$0.002Concurrent or non-blocking apps
Claude 3.5 Sonnet~700ms~$0.0025Advanced coding and reasoning tasks

Quick tip

Define your agent's role clearly in the system message to guide consistent and relevant responses.

Common mistake

Forgetting to set the API key in the environment variable or using deprecated SDK methods causes authentication or runtime errors.

Verified 2026-04 · gpt-4o, claude-3-5-sonnet-20241022
Verify ↗