Agents at scale¶
This guide is for campaigns of many agents, thousands to hundreds of thousands, on an HPC allocation. Each agent's tools run as HPC tasks, its model is served on the allocation, and its state lives in the allocation's memory. The agents run on Flowgentic's own agent loop, on OpenAI-compatible model servers such as vLLM and SGLang. LangGraph is optional: With LangGraph runs the same agents as a LangGraph graph.
Install Flowgentic from PyPI with this path's extra, agents (RHAPSODY and
MCP), and langgraph as well to run LangGraph graphs:
The worked example below runs from a clone, with the locked environment:
git clone https://github.com/stride-research/flowgentic.git
cd flowgentic
uv sync --locked --extra agents
RaDEX, which keeps agents' state in a Dragon distributed dictionary, is not on PyPI, and no extra installs it: its Python client is built from source with its C++ core; see radical-cybertools/radex. A laptop needs neither RaDEX nor Dragon: the worked example below runs without them.
This path or the AsyncFlow path¶
Flowgentic has two paths for agents on HPC resources.
- The AsyncFlow path puts agents inside a workflow. A workflow application calls an agent at fixed decision points, as an AsyncFlow task (Augment an AsyncFlow application), or a LangGraph graph runs its tools and nodes as AsyncFlow tasks (LangGraph on AsyncFlow with Flowgentic). The workflow is in charge, and there are a few agents. This path is in maintenance mode.
- This path makes the agents the campaign. Each agent is a loop of model calls and tool calls, many thousands of them share each Python process, and the process is built for that: its collector, its event loop, its tools, its model calls and its agents' state. Use it when the number of agents, not the workflow, is what the run is about.
The agent process¶
An agent process runs one AgentHost, which runs agents through one runner,
up to max_live_agents at once: a fixed set with run, or one at a time, as
they arrive, with submit (see
Serving agents on demand):
AgentHost.run(agents) starts agents in batches, tunes the collector, resumes
│ suspended agents, counts agents by state
└─ each agent: LoopRunner's loop, on OpenAI-format messages
├─ model call ──► Fleet ──► admission per replica ──► transport ──► model server
├─ tool calls, all of a reply at once
│ ──► inline tool: in this process
│ ──► task tool: a RHAPSODY task in this process's session
│ ──► MCP tool: the tool service, through a supervised pool
└─ suspension ──► its messages, one put ──► RadexStore, or MemoryStore
The application starts everything else: the agent processes, the model servers, the dictionary and the tool service. Flowgentic creates none of them, so the same agent code runs on a laptop and on an allocation.
The parts¶
- Agent host (
flowgentic.host). Runs agents through a runner:flowgentic.loop.LoopRunner, orflowgentic.langgraph.GraphRunnerfor LangGraph. It tunes the cyclic collector while it runs, caps the agents live at once, staggers a run's first wave, resumes agents that wait on tasks, and counts agents by state. One agent's failure never stops the others. - Agent loop (
flowgentic.loop). Runs each agent as a tool-calling loop on OpenAI-format messages: the model replies, the calls of its reply run at once, their results go back to the model, until it answers. Tool errors go back to the model, andmax_model_callsbounds an agent's calls. An agent that calls a task tool that suspends leaves its messages in the store with one put, and reads them back with one get when its task ends; the entry is deleted when the agent ends. - Tool placement (
flowgentic.tools). Each tool says where it runs: inline, as a RHAPSODY task (task_tool), or through the tool service (mcp_tool). A task tool may suspend its agent while the task runs. A call made again when its agent resumes gets the first call's result and runs nothing. - Tool service
(
flowgentic.tools.service). One process calls MCP tools for many agent processes over queues, through a pool of MCP clients that keeps a failed connection from cancelling other calls. - Key-value store (
flowgentic.store). Holds the suspended agents' state: RaDEX on an allocation, a dictionary in the process elsewhere. - Fleet (
flowgentic.fleet). Sends each agent's model calls to one replica, so its prompt stays in that replica's prefix cache, and admits calls while the tokens of live context in flight stay under a cap. A replica is a transport:HTTPTransportfor an OpenAI-compatible server, such as vLLM, SGLang or OpenRouter.
A worked example¶
examples/agents-at-scale/campaign.py runs a whole campaign on a laptop. Each
agent looks its candidate up through an MCP tool, runs a simulation as a
RHAPSODY task and suspends while it runs, takes a note inline, and answers.
Everything around Flowgentic's parts stands in for an allocation: a scripted
model, as a transport, behind a fleet of two replicas, RHAPSODY on a thread
pool, an in-memory store, the tool service in a thread, and an MCP server in
a process of its own on 127.0.0.1.
uv run --extra agents python examples/agents-at-scale/campaign.py --agents 1000
uv run --extra agents python examples/agents-at-scale/campaign.py --agents 1000 --processes 2
With --processes 2, two agent processes each run a host on half the agents,
and both share the one tool service over multiprocessing queues, as agent
processes share it over Dragon's queues on an allocation. CI runs both forms
(tests/integration/test_agents_at_scale.py), from the wheel installed with
the agents extra and no LangGraph.
The tools: one of each kind. The inline one is a plain function.
TOOLS = [
# A RHAPSODY task; the agent suspends while it runs.
task_tool(simulate, suspend=True),
# An MCP tool, through the tool service. A lookup can safely run twice.
mcp_tool(
"lookup",
description="Reference data for a candidate.",
args_schema=LookupArgs,
idempotent=True,
),
# Inline, in the agent's process: a plain function.
note,
]
Each agent process builds its parts. With --dragon (see
Running under Dragon), the session runs on RHAPSODY's
native Dragon backend, the store is RadexStore.attach(descriptor) and the
queues are Dragon's; on an allocation each replica is an
HTTPTransport(model, base_url=...) of a model server:
client = ToolServiceClient(requests, replies, index=index)
client.start()
backend = await task_backend(workers, dragon)
session = Session(backends=[backend])
if telemetry is not None:
await session.start_telemetry(**telemetry.rhapsody_options())
runtime = ToolRuntime(session, client)
# The processes split every replica's caps exactly: this one holds its
# part, at least one request, so at most 2 × 64 processes. On an
# allocation, one replica per model server, with its server's caps:
# [HTTPTransport("Qwen3-8B", base_url=url) for url in servers]
replicas = (
[ScriptedModel(latency_s), ScriptedModel(latency_s)]
if graph is None
else graph.replicas(latency_s)
)
fleet = Fleet(
replicas,
admission=TokenAdmission(tokens=40_000, requests=REQUESTS),
process=(index, processes),
)
# The agents' state: RaDEX on Dragon, this process's memory on a laptop.
store = RadexStore.attach(descriptor) if dragon else MemoryStore()
The runner: Flowgentic's loop over the fleet, the tools' declarations, the runtime they run on, and the store.
# The loop puts an agent's state in the store when the agent suspends,
# reads it back when it resumes, and deletes it when it ends.
agent_runner = LoopRunner(fleet, TOOLS, runtime=runtime, store=store)
And its host, which builds each agent's input as a slot opens, runs the
process's share of the agents, each starting a trace of its own (see
Live dashboard), and then
stops the tool service client, closes the RHAPSODY session and awaits the loop
runner's aclose():
host = AgentHost(
agent_runner,
max_live_agents=5_000,
stagger=Stagger(batch=100, interval_s=0.05),
on_done=on_done,
)
def input_for(i: int) -> dict:
task = f"Find the best temperature for cand-{i:06d}"
return {"messages": [{"role": "user", "content": task}]}
# Each agent starts a trace of its own, sampled on its own, whatever span
# is current: RHAPSODY 0.7's start_telemetry() left its session span current.
root = otel_context.attach(trace.set_span_in_context(trace.INVALID_SPAN))
replies: dict[str, int] = {}
try:
indices = range(index, agents, processes)
if arrivals is None:
report = await host.run_indexed(indices, input_for, id_prefix="agent-")
else: # the agents arrive one at a time: see on_demand()
report, replies = await on_demand(
host, indices, input_for, arrivals / processes
)
finally:
otel_context.detach(root)
client.stop()
await session.close()
if graph is None:
# Waits for what a cancelled agent's put left on its way to the store.
await agent_runner.aclose()
if telemetry is not None:
telemetry.shutdown() # exports what is left
The loop deletes an agent's state from the store when the agent ends, however it ends, and tells the fleet that the agent makes no more model calls. The store then holds the agents that are suspended, or running since a suspension, not every agent of the campaign, which a Dragon dictionary needs: its size is fixed when it is created.
The loop calls the store off the event loop, so that a slow store holds only
the agents that wait for it. It awaits a store's async calls
(AsyncKeyValueStore: RadexStore has them), and calls the other stores from
store_threads threads, 8 by default. A store without async calls must
therefore be safe to call from several threads at once: MemoryStore and
RadexStore are; a store of your own needs a lock around what its calls
share, or LoopRunner(..., store_threads=0), which calls it on the event loop,
one call at a time. An agent that is cancelled, or that its host ends while it
is suspended, has its state deleted on the event loop before its host returns,
as in 0.7.0; RadexStore tries a sync call on the loop once, so the loop waits
for one call's timeout at most per agent: cancelling N agents whose manager
does not answer holds it for up to N timeouts, as in 0.7.0. A put that was
still on its way when the agent was cancelled is the exception: it goes on, and its delete follows in the
background. A new run of that agent id waits for both, and a process that ends
while such a put is in flight should await runner.aclose(), as the example
does, so that the delete is not lost.
--json prints the whole summary, with each process's HostReport figures,
the fleet's, the tools' and the loop's counters.
Serving agents on demand¶
A campaign knows its agents up front. A server does not: an agent starts when
a message arrives, such as a request from another agent over a protocol like
A2A, and the server answers the message when the agent ends. A host started
once takes such agents one at a time, with everything run gives them but
the stagger: the cap, the collector's settings, suspension, the accounting
and on_done. It starts each agent as it comes, unless it is given a
stagger, as this example's host is.
With --arrivals RATE, the example's agents arrive one at a time, RATE a
second on average across the processes, and each process's host takes each
as it comes:
Each message gets a handler, which gives the host its agent and waits for the agent's end:
async def on_demand(
host: AgentHost, indices: range, input_for: Any, rate: float
) -> tuple[HostReport, dict[str, int]]:
"""Agents arrive one at a time, ``rate`` a second on average, as messages
reach a server; the host, started once, takes each as it comes."""
arrival = random.Random(0)
# The block starts the host and stops it at its end; on an error, it
# cancels what is live and drops what waits.
async with host:
handlers = []
for i in indices:
await asyncio.sleep(arrival.expovariate(rate)) # the next message
agent = Agent(f"agent-{i}", input_for(i))
handlers.append(asyncio.create_task(handle(host, agent)))
replies = Counter(await asyncio.gather(*handlers))
report = await host.stop() # waits for the live and waiting agents
return report, dict(replies)
async def handle(host: AgentHost, agent: Agent) -> str:
"""One message's handler: its agent's answer, or why there is none."""
try:
# Never blocks: the agent starts as soon as a slot is free.
future = host.submit(agent)
except QueueFull: # with max_queued, when too many wait for a slot
return "busy, retry later"
result, output = await future # what on_done receives
return answer(output) if result.status == "done" else result.status
async with host:starts the host's period, with the collector's settings, the probes and the accounting, asrundoes, and stops the host at the block's end: cancelling its agents, if the block raised, so that an error never leaves the collector tuned.submit(agent)never blocks. It returns a future of(result, output), whaton_donereceives; the agent starts as soon as a slot is free, after the agents submitted before it. An agent's error is in its result, never raised.- With
max_queued,submitraisesQueueFullonce that many agents wait for a slot, and the handler answers "busy, retry later" rather than pile the work up. stop()refuses new agents, except those the host's own agents submit, waits for the live and waiting ones, and returns theHostReport;stop(drain=False)cancels them instead.
An agent's id is free again once it has ended, so a server can run one
logical agent once per message under one id. The rest, cancelling one agent,
ids, contexts and the report, is on the
Agent host page. CI runs the
example with --arrivals in one and two processes, on the loop and on
LangGraph (tests/integration/test_agents_at_scale.py).
Choosing the limits¶
The host's report says where the agents spent their time, and what running them cost the process. Read it on a run of your own agents, at the scale of one process, before choosing the limits.
These figures come from the example with 1,000 agents on a laptop (an Apple M4 Max, 14 cores, 36 GB of memory, Python 3.11, with other work running). Each column is one run of three, the one with the median time:
| One agent process | Two agent processes | |
|---|---|---|
| Agents done | 1,000 of 1,000 | 1,000 of 1,000 |
| The hosts ran for | 1.7 s | 1.2 s |
Agent-seconds working (WORKING) |
0.5 | 0.5 |
in a tool call, the lookup (TOOL_CALL) |
628 | 501 |
waiting for model admission (MODEL_QUEUED) |
0.02 | 5.2 |
at the model (MODEL_REQUEST) |
20 | 21 |
waiting for a task (TOOL_WAIT) |
13 | 15 |
| Share of the agents' lifetimes accounted for | 100.00 % | 100.00 % |
| Event loop lag, time-weighted p99 and maximum | 0.0 and 0.7 ms | 0.0 and 0.5 ms |
| Collector pauses, in all | 0.03 s | 0.07 s |
- Agent-seconds by state (
report.states). Every agent is in one state at a time, so working, waiting and making model requests add up to the agents' lifetimes; in these runs, to within 0.0001 %. The agents spent most of their time inTOOL_CALL, waiting for the lookup: the one tool service, in a thread of the first process, served a thousand agents through two MCP clients. Their own work (WORKING) took half an agent-second in all. Read the states to find what to move, or add, first: here, the tool service's clients. - The event loop's lag (
report.loop_lag). How late the loop woke a 100 ms timer: the time-weighted 99th percentile, the maximum, and the share of time it ran over 100 ms late. A loop that is late delays every agent in the process. Lowermax_live_agentsuntil the lag is what your agents can bear, then run more processes. - The collector's pauses (
report.collector). With the host's settings the collector paused for a fraction of a second over the whole run. If its pauses grow, raiseCollector(threshold=...)or check that the freeze happened (collector["freeze"]["frozen"]). - Model admission.
TokenAdmission(tokens, requests)caps each replica's tokens of live context and requests in flight, across all agent processes; withFleet(process=(index, processes))each process holds its exact part of them (see Fleet). Settokensto what the server's KV cache holds, and watchMODEL_QUEUEDagainstMODEL_REQUEST: agents queued while the server is not full mean a cap that is too low. - Task workers. Agents in
TOOL_WAITwait for their tasks. If they wait longer than the tasks run, the session's backend needs more workers.
Running under Dragon¶
On an allocation the application runs under Dragon, and creates the same parts on Dragon's resources:
- The dictionary. RHAPSODY's
DragonDataBackendcreates a Dragon distributed dictionary; its endpoint'sserialize()gives the descriptor. Each agent process attaches once, withRadexStore.attach(descriptor), and passes the store to itsLoopRunner, or to LangGraph'sKeyValueSaver. The loop awaits the store's async calls, so a manager on a slow node holds only the agents whose keys it holds; tools that call the store from the event loop should await them too (await store.aget_tensor(key)). Each process attaches up toclientsmore Dragon clients for those calls (8 by default), and one to hash keys. A call that times out is made again untildeadlineseconds (600) have passed since it was made; a sync call on the event loop's thread, asKeyValueSaver's, is tried once. - The tasks. Each agent process creates a RHAPSODY
Sessionon a Dragon execution backend, and passes it to itsToolRuntime. - The tool service. One Dragon process runs
serve(requests, replies, pool)on Dragon queues: one request queue, and one reply queue per agent process. Each agent process starts itsToolServiceClienton its own reply queue. - The agent processes. Each runs one host on a disjoint range of agents
(
run_indexed), or takes its agents as they arrive (startandsubmit), and a fleet withprocess=(index, processes)over the model servers' replicas: withlayout="pack", each process sends to a few of them. - The model servers run on the allocation's GPUs, and each replica is an
HTTPTransportof one server.
Flowgentic imports nothing from Dragon: the application passes in the queues, the session and the store.
The example does all of this with --dragon, under dragon. Only its model
stays scripted:
It needs Dragon, RaDEX's Python client, and a RHAPSODY with the native Dragon
backend, DragonNativeExecutionBackend. RHAPSODY sends a task's function to
its workers with cloudpickle, which sends a function by name when its module
looks importable; the workers cannot import the example, so the example has
cloudpickle send its functions by value. An application whose task functions
live in an installed package needs nothing of the kind. The example also sets
DRAGON_PATCH_MP again after importing RHAPSODY's backends: the import loads
Dragon's LLM engine, which clears it, and processes started through Dragon's
multiprocessing need it
(radical-cybertools/rhapsody#159).
CI runs the example without Dragon. For 0.7.0 the parts were tested under
Dragon by hand: the store's contract on RaDEX, an agent suspended by one
Dragon process and resumed by another, the tool service in a Dragon process
serving two agent processes over Dragon's queues, and the worked example
adapted to Dragon, with 1,000 agents in two processes, on the loop and with
--runner graph, every suspension resumed from RaDEX, and with 200 agents
arriving one at a time (--arrivals). They passed under
Dragon 0.15.0 on Python 3.12, with RHAPSODY at commit 9e103fd, before
RHAPSODY 0.8.0 released its native Dragon backend, and RaDEX at commit
9f9b0df. For 0.7.1, the tests marked dragon passed under Dragon 0.15.0 on
Python 3.12, with RHAPSODY 0.8.0 from PyPI and RaDEX at commit 50449a0.
See Project status.
With LangGraph¶
With the langgraph extra, the same agents run as a LangGraph graph, in the
same host, on the same tools, fleet and store:
uv sync --locked --extra agents --extra langgraph
uv run --extra agents --extra langgraph python examples/agents-at-scale/campaign.py --agents 1000 --runner graph
examples/agents-at-scale/campaign_langgraph.py holds the graph. It calls the
fleet directly from its model node, with the agent's id, and sends each tool
call to the tool node as its own task, with
flowgentic.langgraph.route_tool_calls, so a resumed agent runs no other call
again:
def build_graph(fleet: Fleet, tools: list, saver: KeyValueSaver):
"""model -> each tool call as its own task -> model, until it answers.
The replicas are LangChain chat models, so the fleet passes the graph's
messages and tools to them as they are, and the reply's ``raw`` message is
the model's own ``AIMessage``."""
async def model(state: MessagesState, config: RunnableConfig) -> dict:
agent = config["configurable"]["thread_id"]
reply = await fleet.ainvoke(agent, state["messages"], tools=tools)
return {"messages": [to_ai_message(reply)]}
g = StateGraph(MessagesState)
g.add_node("model", model)
g.add_node("tools", ToolNode(tools, handle_tool_errors=True))
g.add_edge(START, "model")
g.add_conditional_edges("model", route_tool_calls, ["tools", "__end__"])
g.add_edge("tools", "model")
return g.compile(checkpointer=saver)
Its runner: bind_tools turns the declarations into LangGraph's tools, and
KeyValueSaver keeps the graph's checkpoints in the store
(Checkpointer). With GraphRunner's exit
durability an agent writes a checkpoint when it suspends and when it ends;
delete_finished=True deletes each agent's thread once the agent finishes,
as the loop deletes its state; see the
agent host for what the option gives up.
saver = KeyValueSaver(store)
bound = bind_tools(
runtime, [t if isinstance(t, (TaskTool, McpTool)) else tool(t) for t in tools]
)
# A Dragon dictionary's size is fixed: keep the agents still running, not
# every agent of the campaign.
runner = GraphRunner(build_graph(fleet, bound, saver), delete_finished=True)
Its replicas are LangChain chat models, which the fleet runs through
LangChain's adapter (flowgentic.langchain); HTTPTransport replicas work
too, through fleet.chat_model(). Agent loop
compares the two runners, and measures them on this example's workload. An
application that moves its agents from the graph to the loop sees two
differences: a task tool's suspend is asked before the task is submitted,
not after prepare, and the prompts' lengths can differ by a few tokens per
call, because the loop renders the tools' schemas through pydantic and
LangChain with its own converter (Compared with
GraphRunner).
Telemetry¶
Each part records OpenTelemetry spans and metrics through the API; the
application configures the SDK and its exporters, as
Telemetry describes. Each agent's run is an
invoke_agent span, each task or service call an execute_tool span, and
the agents by state, the host's loop lag and collector pauses, the fleet's
admission and the tools' durations are metrics. Each feature page lists its
own.