AI Agents Developers OpenAI

How to Build a Long-Running Agent With OpenAI's Agents API

Most agent demos run for seconds. A long-running agent has to survive a crash, resume where it left off, and know when to stop on its own. Here's how OpenAI's Agents API and AgentKit handle the four problems that actually matter for multi-hour or multi-day runs.

Abstract glowing network of connected nodes on a dark background, soft blur, no legible text
A long-running agent is really a small state machine: steps, checkpoints, and a condition to stop.
Laptop screen glowing softly with abstract colorful blocks, out of focus, developer typing in dim room
A long-running agent needs somewhere durable to persist its state between steps, not just a single chat context.
Updated September 2026 By the AI Help Guide editorial team 9 minute read
4
core building blocks every long-running agent needs
Oct 2025
when OpenAI's AgentKit and visual Agent Builder launched
0
the cost ceiling should never be, for an unattended agent
Hours-Days
the realistic scope for a genuinely long-running agent run

What "Long-Running" Actually Means for an Agent

Most agent demos run for seconds: you send a prompt, a couple of tool calls happen, and you get an answer. A long-running agent is built to work for minutes, hours, or across days — researching a topic, migrating a codebase, or monitoring something and reporting back when a condition is met. That changes the engineering problem entirely. Instead of one request-response cycle, you need a process that can survive a crash, resume from where it left off, and stop on its own once it either finishes or hits a limit you set.

OpenAI's Agents API and the surrounding Agents SDK give you the building blocks for this: models, tools, memory, guardrails, and orchestration, wired together so an agent can call functions, hand off to a specialized sub-agent, and keep going across many steps rather than one shot. AgentKit, OpenAI's broader agent-building suite, adds a visual Agent Builder on top for designing multi-step workflows without writing all the orchestration code by hand.


Four Things Every Long-Running Agent Needs

Hard Stop Conditions

Cap total cost, step count, or wall-clock time so the loop always terminates on its own instead of running indefinitely on a stuck task.

Persisted State

Save conversation and task state somewhere durable — a database, object storage, or the filesystem — so the agent can resume after a crash, a deploy, or a human approval pause.

Progress Streaming

Stream events out of the loop as it works, so a dashboard or a person watching can see what's happening rather than waiting in the dark for a final answer.

Handoffs Between Specialized Agents

Split a long task across agents built for different jobs — a research agent, a drafting agent, a review agent — passing state between them instead of asking one agent to do everything.


A Practical Setup Path

Start smaller than you think you need to. Get a single-step agent working reliably with the Agents SDK first — one tool, one clear success condition — before adding the complexity of multi-hour runs. Once that's solid, add a persistence layer so the loop's state survives a restart, then add your cost and step ceilings before you ever let it run unattended. Guardrails, OpenAI's term for the checks that constrain what an agent is allowed to do, should go in before the long-running behavior does, not after; it's much harder to retrofit safety limits onto a process that's already been running autonomously for hours.

If you don't want to hand-write the orchestration loop, AgentKit's Agent Builder lets you compose a multi-step workflow visually — templates, nodes, and a preview run — and export it to code once you're happy with the shape of it, which is a faster path to a working prototype than building the state machine from scratch.


Where This Gets Genuinely Hard

The failure mode that catches people off guard isn't the model getting an answer wrong — it's the agent running for hours on a task that quietly went sideways early on, burning cost and producing a large amount of confidently wrong output before anyone notices. Cost ceilings and step limits aren't optional hardening for a long-running agent; they're the difference between a bug that costs a few cents and one that costs real money while you were asleep.

Before you deploy anything unattended: test the resume-after-crash path deliberately, not just the happy path. An agent that works perfectly until the exact moment it needs to recover from a failure isn't actually production-ready for long, unattended runs.

What People Actually Build This For

Multi-Day Research Projects

Kick off a research task that keeps working across sessions, checking back in with a report once it's substantially further along.

Large-Scale Code Migrations

Work through a big, repetitive refactor file by file, with checkpoints so progress survives an interruption.

Background Monitoring Agents

Watch a data source or condition over time and only surface a result once something meets a threshold you defined.

Voice-Driven Assistants

Combine a long-horizon loop with speech input and output for hands-free, ongoing task delegation.


Where Long-Running Agent Projects Actually Fail

The most common mistake isn't a modeling problem at all — it's skipping the persistence layer because the first prototype works fine without it. A quick demo running in a single process for a few minutes doesn't need durable state, so it's easy to defer building it, and then genuinely hard to retrofit once the agent is already deployed and running unattended. Persistence is far cheaper to design in from the start than to bolt onto a system that's already live.

The second common failure is under-specifying the stop conditions. "Stop when the task is done" sounds reasonable until an agent gets stuck in a loop that technically never satisfies its own completion check, quietly consuming cost and compute for hours before anyone notices. Concrete, boring limits — a maximum step count, a maximum dollar spend, a maximum wall-clock duration — catch this failure mode even when the more elegant, task-aware completion logic doesn't.

A third mistake worth naming: building the multi-agent handoff structure before confirming any single agent in the chain actually performs its individual step reliably. Debugging a five-stage pipeline where one stage is subtly unreliable is much harder than debugging that one stage in isolation first, then composing the pipeline once every individual piece is already trustworthy.


The Gap Between a Working Demo and a Deployable Agent

A long-running agent that works reliably in testing still needs a genuine observability layer before it's trustworthy running unattended in production — logs of every step it took, every tool it called, and every decision point along the way, searchable after the fact when something needs investigating. Without this, debugging a multi-hour run that went wrong means trying to reconstruct what happened from a final output alone, which is far harder than reviewing a clear step-by-step trace.

It's also worth building in a manual kill switch from day one, separate from the automatic stop conditions. Automatic limits catch the failure modes you anticipated; a kill switch that a person can trigger immediately covers the ones you didn't, and having it in place before the first genuinely unattended run — not added reactively after a problem — is the difference between a minor incident and a much longer one.

Bottom Line

A long-running agent is less about a smarter model and more about disciplined engineering: hard stop conditions, durable state, visible progress, and handoffs between specialized agents. Get a single-step agent solid first, then add the long-horizon behavior on top of guardrails that were already in place.

More AI Guides, Written Clearly

Browse our full library of AI tool guides, updated regularly as models and features change.

Common Questions

What's the difference between a normal agent and a long-running one?

A normal agent completes a task in one request-response cycle. A long-running agent is built to keep working across minutes, hours, or days, with the ability to resume after an interruption.

Do I need AgentKit to build a long-running agent?

No — you can build the orchestration loop directly with the Agents SDK. AgentKit's Agent Builder is a faster, visual way to compose the same kind of workflow without hand-writing all of it.

What happens if a long-running agent crashes mid-task?

That depends entirely on whether you've built in persisted state. Without it, the agent starts over from scratch; with it, the agent can resume from its last saved checkpoint.

How do I stop a long-running agent from running away with cost?

Set hard ceilings on total cost, step count, or wall-clock time before you ever let the agent run unattended, so the loop terminates on its own once any limit is hit.

Sources: OpenAI — Introducing AgentKit · OpenAI Building Agents Guide (mirrored)
OTHER PROJECTS

More from the creator

Explore two other projects from the same creator.

Didn't find exactly what you needed here? Chat with our AI — it can search and point you to the right guide.