OpenAI AI Agents 8 min read

OpenAI Assistants API Guide 2025: Build Persistent AI Agents

The Assistants API is OpenAI's managed platform for building AI agents with persistent memory, built-in tools, and file access. It handles thread management, context windows, and tool execution so you don't have to.

Core Concepts

Assistant

A configured AI persona with a model, instructions (system prompt), and enabled tools. Reusable across many threads. Create once, use everywhere.

Thread

A conversation session. Stores all messages. OpenAI automatically truncates old messages to fit within the model's context window — you never have to manage token limits manually.

Message

A user or assistant message within a thread. Can include text, images, or file attachments. Messages are appended to the thread and persist until the thread is deleted.

Run

Activating an Assistant on a Thread. The run goes through: queued → in_progress → (requires_action if tools needed) → completed. You stream or poll for completion.

Create an Assistant & Run a Thread

from openai import OpenAI

client = OpenAI()  # uses OPENAI_API_KEY env var

# 1. Create an Assistant (do this once, save the ID)
assistant = client.beta.assistants.create(
    name="Data Analyst",
    instructions="You are a data analyst. Answer questions about the uploaded CSV files.",
    model="gpt-4o",
    tools=[{"type": "code_interpreter"}, {"type": "file_search"}],
)
print(assistant.id)  # asst_abc123 — save this

# 2. Create a Thread (once per user session)
thread = client.beta.threads.create()

# 3. Add a Message
client.beta.threads.messages.create(
    thread_id=thread.id,
    role="user",
    content="How many rows are in my dataset?",
)

# 4. Stream the Run
with client.beta.threads.runs.stream(
    thread_id=thread.id,
    assistant_id=assistant.id,
) as stream:
    for text in stream.text_deltas:
        print(text, end="", flush=True)

Built-in Tools

Code Interpreter

Runs Python in a sandboxed environment. Analyzes uploaded CSVs/Excel files, generates charts, performs math, and processes data. Costs $0.03 per session (a session lasts up to 1 hour).

File Search (Vector Store)

Upload PDFs, Word docs, text files. OpenAI automatically chunks, embeds, and indexes them in a vector store. The assistant retrieves relevant chunks on demand. Pricing: $0.10/GB/day after the first 1 GB free, plus $2.00 per 1,000 searches.

Function Calling

Define custom functions (JSON schema). The assistant decides when to call them and returns a requires_action run status. You execute the function and submit the result back.

Uploading Files & Vector Stores

# Upload a file
with open("report.pdf", "rb") as f:
    file = client.files.create(file=f, purpose="assistants")

# Create a vector store and add the file
vector_store = client.beta.vector_stores.create(name="Q1 Reports")
client.beta.vector_stores.files.create(
    vector_store_id=vector_store.id,
    file_id=file.id,
)

# Attach the vector store to the assistant
client.beta.assistants.update(
    assistant_id=assistant.id,
    tool_resources={
        "file_search": {"vector_store_ids": [vector_store.id]}
    },
)

Assistants API vs Chat Completions API

Factor Assistants API Chat Completions
Message history Managed by OpenAI (Threads) You manage manually
Context window Auto-truncated You handle truncation
Built-in tools Code Interpreter, File Search None (DIY)
Latency Higher (managed overhead) Lower (direct)
Streaming Yes (since 2024) Yes
Best for Agents with memory and files Simple completions, full control

Pricing

  • Tokens: Same rates as Chat Completions (GPT-4o: $2.50/$10.00 per 1M in/out)
  • Code Interpreter: $0.03 per active session (up to 1 hour)
  • Vector store storage: $0.10 per GB per day (first 1 GB free)
  • File Search tool calls: $2.00 per 1,000 calls
  • Vector stores expire after 7 days of inactivity by default — set expires_after to control this

Monitor OpenAI API Status

Assistants API runs depend on OpenAI uptime. Get instant alerts when OpenAI degrades — before your users notice.

Check OpenAI Status →