Workflow runs
Follow an agentâs workflow runs: their states and events, when the work is done, requests while a run is open, budgets, and limits.
A workflow is a program that an agent writes to run many agents and combine what they return. A workflow run is the execution of one workflow. Dynamic workflows is the feature that lets an agent write workflows and start runs. You turn it on or off with the workflows setting in the agentâs multiagent block.
The server runs a workflow in the background. Its agents work in session threads that the server creates as the workflow needs them. You follow runs on the sessionâs event stream. To stop a run, ask the agent to stop it (see Interrupt a session with runs open).
How dynamic workflows work
The agent that the session runs writes each workflow for the work that you describe. A workflow is a program: it runs other agents, collects what each one returns, and combines the results. That way, the agent can take on a task that is too large for one conversation, such as a review of hundreds of documents. During a run, the agent can keep working or end its turn, and it can check on the run.
The diagram shows one example. Each workflow that the agent writes has its own phases and agents. A run has these layers:
- Workflow run: The server runs the workflow in the background, as one workflow run. A session can have several runs open at the same time.
- Phases: A workflow can divide its work into phases. A phase is a named stage of the run, such as âRead the contracts.â You follow a runâs progress by its phase events.
- Agent threads: In a phase, the program runs agents. Each agent works in its own session thread, on a prompt that the program wrote. An agent in a run can be an inline agent, which the program defines itself. It can also be a predefined agent, which you list in the
predefined_agentslist ofworkflows. For what each thread shows, see A runâs threads.
The program can do the following:
- Run agents at the same time: The program can run many agents at the same time, which is called fanning out. In the diagram, three agents read contracts in the first phase.
- Pass results from one agent to another: Each agent returns its result to the program. The program can pass that result on to another agent. In the diagram, the agent in the second phase works with what the first three returned. A runâs agents also work with the same files, in the sessionâs sandbox.
- Take the next step by itself: An agentâs result goes to the program, not to the agent that the session runs. The program determines which agents run next, and it writes their prompts.
- Repeat and choose: Inside a phase, the program can repeat work and choose its next step from what an agent returned. For example, it can have a draft revised until a review passes or a set number of rounds is used up. In the diagram, the program can repeat a step inside the second phase.
- Handle a failed agent: When one of its agents fails, the server usually runs that agent once more, on a new thread, while the runâs other agents keep working. If it fails again, the program can handle the failure or let it end the run. If the program handles a failure, the run can still end
completed, so tell the agent that the session runs to report what failed.
When the run ends, the agent that the session runs gets a turn to read what the run did. It can then answer you or start another run. Run events lists the cases where that turn comes later or doesnât come.
You can guide how a run does the work, for example, how it splits the work and what it does when an agent fails. See Tell the agent when to use a run.
Thereâs no approval step before a run starts: the agent starts a run in the same turn that it writes the workflow. To review a plan first, tell the agent to describe the run and wait for your approval. As a run starts, workflow_run.created shows its name, description, and phases. If a run starts that you didnât approve, ask the agent to stop it (see Interrupt a session with runs open).
How a run moves through its states
A run starts as running or as idle. Reaching the budget, for example, pauses a running run, which makes it idle. Raising or removing the budget then makes it run again, unless an interrupt paused it too. A running run ends when its workflow completes, the agent stops it, it fails, its lifetime passes, or the session is archived. An idle run can end too, for example, when the agent stops it or the session is archived.
A run is open from its workflow_run.created event until its workflow_run.status_ended event, whether itâs running or idle. A run is idle while itâs paused, for example, at the sessionâs budget. A run has a lifetime limit, which the agent can set when it starts the run. Time that a run spends waiting on your client counts toward that lifetime. A pause doesnât stop a runâs lifetime from passing, so a run that stays paused can end with timeout_error. The following events report a runâs start, its phases, and its end. A pause at the budget sends one too. A pause after an interrupt might send none. Every workflow_run.* event includes workflow_run_id, which is None only on a workflow_run.error when no run was created.
Run events
Run events arrive on the sessionâs event stream, which is the primary threadâs stream, and listing the sessionâs events returns them too. Run events donât trigger webhooks. Status events from the runâs threads arrive on the same stream. Each names its thread in session_thread_id, and a runâs threads are the ones whose session.thread_created event had the runâs workflow_run_id.
| Event | When it arrives | What to do |
|---|---|---|
workflow_run.created | The agent started a run. Includes workflow_run_id (wrun_âĶ), the runâs name and description, and phases, the phases the workflow declares, each with an id, a name, and a description. A description is None when the workflow gives none. phases is always present and can be empty. The runâs and phasesâ name and description are text the model wrote, so they can repeat words from your request. A runâs name can also be one the server assigned. | Track the run as open. Show its name, and progress against phases. |
workflow_run.status_running | When the run starts its work, which can be a while after created, and each time it resumes after a pause at the budget. A resume after an interrupt might not send it. A run that starts idle might get workflow_run.status_idle first. | Show the run as running. |
workflow_run.status_idle | The run paused, for example, at the sessionâs budget. The event doesnât say why. A pause after an interrupt might not send it. | To continue, see Cost and budgets or Interrupt a session with runs open. |
workflow_run.phase_started, workflow_run.phase_ended | The workflow entered or left a phase, or the runâs end closed a phase that was still open. The end event doesnât say whether the phaseâs work completed. Both include workflow_run_phase_id. The end also has phase_started_id, the id of the start event it closes. Neither has the phaseâs name: look it up by workflow_run_phase_id in the phases of workflow_run.created. | Update progress. Phases run one at a time, in the order of phases, each at most once, but the API doesnât guarantee it. Match a phaseâs end to its start by phase_started_id. Handle more than one open phase, a phase that isnât in phases, and a listed phase that never starts, even in a run that completes. Every phase that starts also ends, before the runâs workflow_run.status_ended. |
workflow_run.status_ended | The run ended. Always the last of the runâs workflow_run.* events. Includes result. | Read result (next table). The agent then gets a turn to read how the run ended. At the budget, or while the primary thread waits on your client, that turn comes later. After an interrupt, that turn might not come: send a user.message, or read result yourself. After an archive or termination, it doesnât come. |
workflow_run.error | The server reports an error of a run, or a start that it refused. A run that ends in error gets this event, with the same error, before its workflow_run.status_ended. Includes error: a type and a message that is safe to log. workflow_run_id is None when no run was created. | Log it, and donât take it as the runâs end. If workflow_run_id is None, no run started. Otherwise keep tracking the run until its workflow_run.status_ended. |
result | Meaning |
|---|---|
{"type": "completed"} | The workflow finished running. The result doesnât say whether the work passed. A run can end completed even though work on its threads failed, or a thread couldnât be created. To find failed work, read the events of each of the runâs threads. |
{"type": "stopped"} | The agent stopped the run, or the session was archived. The event doesnât say which. Donât treat these as the only causes. |
error with timeout_error | The run reached its lifetime limit, which the agent can set. |
error with program_error | The workflow failed. Its code failed, or it broke a rule for workflows, other than a limit. Or one of the runâs threads failed, or couldnât be created, and the workflow let that end the run. |
error with thread_limit_error | The run went over its limit on the agents that a workflow starts. |
error with unknown_error | The server couldnât continue the run, or the run went over one of the serverâs other limits on workflows. |
An error result looks like {"type": "error", "error": {"type": "timeout_error", "message": "..."}}, where message is safe to log. Treat an unrecognized result.type as a run that ended some other way, and an unrecognized error.type as an error. When something the session depends on fails, such as the model, an MCP server, credentials, or billing, the failing threadâs stream gets a session.error. That doesnât end a run by itself. But if it makes one of the runâs threads fail, and the workflow lets that end the run, the run ends with program_error.
For example, you ask the contract-review agent which of 300 contracts have a change-of-control clause, and the agent starts a run:
workflow_run.creatednames the run âFind change-of-control clausesâ and lists the phases âRead the contractsâ and âReconcile the findingsâ inphases. Thenworkflow_run.status_runningfollows.- Phase events mark each phase, and each thread the run creates sends
session.thread_createdwith the runâsworkflow_run_id. workflow_run.status_endedarrives withresult: {"type": "completed"}.- The agent answers, â41 of the 300 contracts have one,â and
session.status_idlearrives withend_turn.
The runâs first event lists its phases:
{
"type": "workflow_run.created",
"id": "sevt_01abc...",
"workflow_run_id": "wrun_01J8XkN5uT3vHpLqRfWdY2",
"name": "Find change-of-control clauses",
"description": "Reads each contract and lists those that have the clause.",
"phases": [
{
"id": "wrph_01Kd3a1f3",
"name": "Read the contracts",
"description": "Reads each contract for the clause."
},
{ "id": "wrph_01Kd3b7c9", "name": "Reconcile the findings", "description": null }
],
"processed_at": "2026-10-09T14:01:45Z"
}Each phase event names its phase by workflow_run_phase_id. That is an id in phases, but the API doesnât guarantee it:
{
"type": "workflow_run.phase_started",
"id": "sevt_01def...",
"workflow_run_id": "wrun_01J8XkN5uT3vHpLqRfWdY2",
"workflow_run_phase_id": "wrph_01Kd3a1f3",
"processed_at": "2026-10-09T14:01:46Z"
}The runâs last event reports how it ended:
{
"type": "workflow_run.status_ended",
"id": "sevt_01ghi...",
"workflow_run_id": "wrun_01J8XkN5uT3vHpLqRfWdY2",
"result": { "type": "completed" },
"processed_at": "2026-10-09T14:09:12Z"
}A runâs threads
Each agent in a run works in its own session thread, which the server creates as the workflow needs it. You can list, read, and stream a runâs threads like any child thread, and answer their tool calls from the primary stream. To stop them, ask the agent to stop the run (see Interrupt a session with runs open).
- Grouping: A runâs thread has the runâs
workflow_run_id, as does thesession.thread_createdevent that announces it. Other threads, and thesession.thread_createdevents that announce them, haveworkflow_run_idset toNone. - Agent:
agentshows the agent the thread runs. For an agent you listed in thepredefined_agentslist ofworkflowsinmultiagent,agenthas that agentâsidandversion. The thread of a subagent you listed shows them too. For an agent the workflow defines (an inline agent),agenthastypeinlineand noidorversion. It has the system prompt the workflow wrote, not the session agentâs. It also has the name and description the workflow gave it. The server assigns a name if the workflow gave none. It uses the model of the session agent, the agent the session runs. Its tools, MCP servers, and skills are a subset of the session agentâs. It gets all of them, but the API doesnât guarantee that. Its tools keep their permission policies. - What the threads share: A runâs threads work in the sessionâs sandbox, so every thread works with the same files. That includes the files of a memory store the session mounts. An agent the workflow defines uses its MCP servers with the credentials the session resolves for them. Each thread has its own conversation history.
- Events: A run threadâs
session.thread_created,session.thread_status_running,session.thread_status_idle, andsession.thread_status_terminatedevents also arrive on the primary stream (see Run events). Its message events stay on its own stream. The server sends its thread webhooks as for any child thread. For what the threadâs own stream records, see Session thread events. - Phases: No event or field says which phase a thread works in, and threads of one run can have the same
agent_name. Follow a runâs progress by its phase events, and tell its threads apart bysession_thread_id. - Archiving: The server archives each thread no later than the end of its run. It can archive one sooner, once the thread returns its result or the run finishes with it. If the thread is still running or waiting on your client at that point, the server stops it first. An archived thread stays in the thread list, with status
terminated. You donât need to archive a runâs threads yourself. - Tools: A run can create more than one thread for the same piece of work, so make the tools your agents call safe to call twice.
Know when the work is done
While a run is running, expect the session to stay running, even while none of its threads is working. It goes idle with requires_action when no thread is working and a thread waits on your client. An idle by itself doesnât mean that the work is done. The work is done when both are true:
- Every run youâve seen created has its
workflow_run.status_ended. - After that, a
session.status_idlearrives withstop_reasonend_turn, and your own request, such as an interrupt, didnât cause it. After you interrupt, count only an idle that comes after your nextuser.messageoruser.define_outcome.
- Paused runs: A paused run doesnât keep the session
running, so the session can go idle while the run is still open. At the budget, for example, the session goes idle withbudget_reached. The work isnât done until the run ends. - Another run: The agent can start a new run when it reads a result, so check again.
- Outcomes: If you defined an outcome, no evaluation starts while a run is open, whether itâs running or idle. The turn in which the agent reads the runâs result can start one.
retries_exhausted: The agentâs turn failed on an error: retries ran out, or the error canât be retried, such as a billing failure. A run might still be running when this idle comes. If a run ended and the agent hasnât read its result yet, the server starts a new turn with no input from you. The session goesrunningagain, so wait for the next idle. If the session stays idle, read thesession.errorthat came before it and fix the cause. Then send auser.message, or read each runâsresultyourself.
Follow a run
This sample follows a session from your message to the agentâs answer. It opens the stream and sends the message. Then it does the following:
- Tracks each run from its
workflow_run.createdto itsworkflow_run.status_ended, and prints each phase as it starts. - Answers custom tool calls when each
agent.custom_tool_usearrives, because a runâs thread can wait on your client while the session staysrunning. If your agentâs tools ask for confirmation, add a branch that answers eachagent.tool_useoragent.mcp_tool_usewhoseevaluated_permissionisask. The sample has none, because a branch that allows every call would turnalways_askinto always allow. - Stops when the work is done: no run is open, and the session goes idle with
end_turn. It also stops in these cases:- The session terminates.
- The session goes idle with any stop reason other than
end_turnorrequires_action, such asbudget_reached,retries_exhausted, orrefusal. The sample prints the reason and stops, so handle those in your own code. It stops onretries_exhaustedeven when the server is about to start a new turn by itself.
- Keeps waiting on
requires_action, and onend_turnwhile a run is open.
open_runs: dict[str, str] = {} # workflow_run_id -> run name
phase_names: dict[tuple[str, str], str] = {} # (run ID, phase ID) -> phase name
# Open the stream first, then send the user message
with client.beta.sessions.events.stream(session_id) as stream:
client.beta.sessions.events.send(
session_id,
events=[
{
"type": "user.message",
"content": [
{
"type": "text",
"text": "Which contracts in /contracts have a change-of-control clause?",
},
],
},
],
)
for event in stream:
match event.type:
case "workflow_run.created":
open_runs[event.workflow_run_id] = event.name
for phase in event.phases:
phase_names[event.workflow_run_id, phase.id] = phase.name
print(f"Run started: {event.name}")
case "workflow_run.phase_started":
phase_id = event.workflow_run_phase_id
key = (event.workflow_run_id, phase_id)
print(f" Phase: {phase_names.get(key, phase_id)}")
case "workflow_run.status_ended":
name = open_runs.pop(event.workflow_run_id, event.workflow_run_id)
print(f"Run ended: {name} ({event.result.type})")
case "agent.custom_tool_use":
# Answer when the event arrives. A run's thread can wait on your
# client while the session stays running.
result = call_tool(event.name, event.input)
try:
client.beta.sessions.events.send(
session_id,
events=[
{
"type": "user.custom_tool_result",
"custom_tool_use_id": event.id,
"content": [{"type": "text", "text": result}],
},
],
)
except anthropic.BadRequestError as error:
# The server refuses a result that comes too late, after it
# archived the call's thread. Keep following the run.
print(f" Answer to {event.name} refused: {error.message}")
case "session.status_idle":
# Done when every run has ended and the agent has finished its turn
if not open_runs and event.stop_reason.type == "end_turn":
break
# An idle with requires_action waits on your client, so keep reading.
# On any other stop reason, print it and stop.
if event.stop_reason.type not in ("end_turn", "requires_action"):
print(f"Session idle: {event.stop_reason.type}")
break
case "session.status_terminated":
breakInterrupt a session with runs open
Send user.interrupt with no session_thread_id, or with the primary threadâs ID. It stops the agentâs turn. It ends no run. The sessionâs runs might pause or keep running, and their events might not show which. A paused runâs lifetime keeps passing, so the run can end with timeout_error while itâs paused.
- Waiting tool calls: After the interrupt, a run threadâs tool call might still wait on your client. Answer each one. To cancel a call that asks for confirmation, deny it. To cancel a custom tool call, send a result with
is_errorset totrueand text incontentthat says why. While the session isidlewithrequires_action, auser.messagereturns 400, so answer the calls first. - Stopping the runs: Send a
user.messageasking the agent to stop its runs. A stopped run ends withresult{"type": "stopped"}. While the session isidlewithbudget_reached, auser.messagereturns 400 until you raise or remove the budget. Raising or removing it also resumes the runs that the budget paused, unless the interrupt also paused them. - Continuing the runs: Send a
user.messageasking the agent to continue its runs. After an interrupt, the runs might wait for this message. If the session isidlewithbudget_reached, raise or remove the budget first. - Run results: A run that ends after the interrupt still sends
workflow_run.status_ended.
While a run is open
| Request | While a run is open | What to do |
|---|---|---|
| Archive or delete the session | Might return 400 while a run is open, whatever the sessionâs status. The errorâs error.details.error_code can be "workflow_run_open". Might also succeed. | Ask the agent to stop its runs, or wait until each run has ended. A paused run ends by itself only when its lifetime passes. Then send the request once the session is idle. An archive that succeeds ends each open run with {"type": "stopped"}. After an archive, a runâs workflow_run.status_ended, and the workflow_run.phase_ended of a phase that was still open, donât arrive on the stream. List the sessionâs events to read them. After a delete that succeeds, no workflow_run event reports the end of the sessionâs runs. |
| Archive one of a runâs threads | Returns 400 with error.details.error_code: "workflow_run_open" while the run is open, running or idle, unless the server has already archived the thread. | Nothing. The server archives a runâs threads. |
Update the sessionâs agent | Returns 400 with error.details.error_code: "workflow_run_open" while any run is open, even a paused one. Updating the underlying agent is still accepted, and the session keeps its own copy. A request that also sends other fields, such as budget, is rejected whole. | Wait until every run has its workflow_run.status_ended, or ask the agent to stop its runs. |
| Answer a tool call or tool confirmation from a runâs thread | Allowed. It arrives on the primary stream, and its session_thread_id names the thread. | Answer as soon as the event arrives, passing the eventâs id as tool_use_id or custom_tool_use_id. Donât wait for session.status_idle: the session can stay running while the runâs other threads work. Once the server has archived the thread, a tool result for one of its calls has no effect, and it can return 400. When a tool result returns 400, find the callâs thread in the thread list. If its status is terminated, the result came too late, so drop it. Send each tool result in a request of its own, because the server refuses a whole request when it refuses one of its events. A tool confirmation that comes too late returns 200, which doesnât mean that the tool ran. |
Rebuild run state after you reconnect
Rebuild each runâs state from the sessionâs events. The stream doesnât replay what you missed: a new connection delivers only events emitted after it opened. So list the events with a types filter, one entry for each event type, as in List past events. Pass each responseâs next_page as page until next_page is None or missing. workflow_run.created, workflow_run.status_running, workflow_run.status_idle, and workflow_run.status_ended give each runâs state, except that a run paused after an interrupt might still show as running. workflow_run.phase_started and workflow_run.phase_ended rebuild progress. A run with no status event yet hasnât started running. No endpoint lists runs.
Cost and budgets
A runâs model requests count toward the sessionâs budget. A run has no price of its own. The tokens its agents use are billed like the sessionâs other tokens, at each modelâs rates. For all of a sessionâs charges, see Claude Managed Agents pricing.
- One runâs usage: List the sessionâs threads and add up the token counts in
usageof the threads with the runâsworkflow_run_id. The list includes archived threads, whose status isterminated, so a finished runâs threads are counted. Pass each responseâsnext_pageaspageuntilnext_pageisNoneor missing, and skip a thread with nousage. If you add up the threadsâlist_costinstead, the total leaves out session runtime, and each figure is rounded separately. - At the budget: Every open run pauses, and the session reports
idlewithbudget_reached, orrequires_actionif a tool call is also waiting. Each thread completes the model request it already started, so a run can pass the budget by one request for each working thread. Raising or removing the budget resumes the runs it paused, unless an interrupt also paused them. If the sessionâs usage includes a model with no list price, only removing the budget does; see Models without a list price. A paused runâs lifetime keeps passing, so the run can end withtimeout_errorwhile itâs paused.
Limits
| Limit | Value | At the limit |
|---|---|---|
| Threads working at once in one run | 64 | The run creates no more until one finishes. The API doesnât guarantee this number. |
| Agents a workflow starts over the runâs whole life | 1,000 | When the workflow asks for more, the server doesnât start another agent, and the run ends with thread_limit_error. The server can run a failed agent again on a new thread, so a run might have more than 1,000 threads. |
| Run lifetime | 24 hours by default, or the lifetime the agent sets | The run ends with timeout_error. No event says what lifetime the agent set. |
| Runs open at once in a session | 10 by default | The server refuses to start another run. The agentâs tool call gets an error, and you get a workflow_run.error whose error.type is max_workflow_runs_error. Idle runs count toward the limit. |
The server shortens a runâs or phaseâs name to 64 characters and its description to 256. The server has other limits on workflows, and rules for them, that arenât listed here. What you see depends on when the server finds the problem:
| What occurs | What you see |
|---|---|
| The workflow is over one of the other limits when the agent starts the run | The server refuses the start. You get a workflow_run.error, and no run. |
| The run goes over one of the other limits later | You get a workflow_run.error, and then the run can end with unknown_error. |
| The server finds after the start that the workflow breaks a rule for workflows, other than a limit | You get a workflow_run.error, and the run can then end with program_error. |
A session can start any number of runs over its life.
- Starting runs: Only the agent on the sessionâs primary thread starts runs. An agent working in a runâs thread canât start a run of its own, so runs donât nest.
- Ending runs: No event you send ends a run; archiving the session can.
- A runâs threads: You canât stop a thread by its ID, or archive one while its run is open.
- Thread limit: A runâs threads are exempt from the sessionâs child-thread limit.
- Visibility: You donât see the workflowâs code, but you can ask the agent for the workflow, as the tip in A runâs threads describes. You also donât see the tool calls the agent makes to start and manage runs, or the result each thread returns to the workflow.
Rate limits
A runâs work counts toward rate limits that your organization already has.
| What | Counts toward | What to do |
|---|---|---|
| Your clientâs requests to retrieve or list the session, its threads, and their events | The read limit for Managed Agents endpoints | Follow a run on the sessionâs event stream instead of polling. |
| Model requests from a runâs threads | Your Messages API rate limits for the model each thread uses, along with your other traffic | Leave room for a run in those limits, or request higher limits. |
A threadâs own stream can get a session.error when one of its model requests is rate limited or the model is overloaded. The errorâs type is model_rate_limited_error or model_overloaded_error:
- If its
retry_status.typeisretrying, the server is retrying the request, and the thread is still working. - If itâs
exhausted, the thread has failed. If the workflow lets that failure end the run, the run ends withprogram_error, which doesnât name the cause. Read the failed threadsâ events to find it.
The server also limits how much all of your organizationâs sessions do each minute. A thread that meets this limit stops, with a session.error on its own stream whose message names a rate limit. Wait a minute before you ask the agent to continue.
Availability
Claude API
- Status
- Beta
- API
- Managed Agents
- Beta header
- Requires
- Models
- All models
Claude Platform on AWS
- Status
- Beta
- API
- Managed Agents
- Beta header
- Requires
- Models
- All models
Claude Models on Google Cloud (Anthropic managed)
- Status
- Beta
- API
- Managed Agents
- Beta header
- Requires
- Models
- All models
Data handling
Data handling overviewApplicable endpoints
/v1/agents/v1/sessions/v1/environments
Not eligible for ZDR, except for self-hosted Managed Agents under Legacy ZDR. Sessions are stateful resources; transcripts persist until you delete them. Learn more
Not eligible for HIPAA. Applies to all Managed Agents sub-features.
Was this page helpful?