Lesson 69.1 · 1 of 5 · 4 min read · Reviewed 24 Nov 2026
Horizontal scaling of stateless agents
By the end, you can
- Scale stateless agent workers horizontally.
- Move agent state out of the process.
Model-backed services scale well sideways, if workers hold no state of their own.
Stateless workers
load balancer --> worker A --\
--> worker B ---+--> shared store (conversations, agent state, files)
--> worker C --/ |
+--> model API (rate limited)
Each request carries a conversation or job id. The worker loads state, does one step or one turn, saves state, and is free again.
What to externalise
| State | Where |
|---|---|
| Conversation history | Database keyed by conversation id |
| Agent progress and notes | Job record or a notes file in object storage |
| Uploaded files | Object storage |
| Cached tool results | A shared cache with a TTL |
The real limits
Adding workers helps until you hit the model provider’s rate limitsRate limit: A cap on how many requests or tokens you can use in a period, such as per minute. or your budget. Past that point, more workers only create more 429 errors429 error: The HTTP status for too many requests: you hit a rate limit.. Size the worker pool to your rate limits (69.2), and scale on queue depth rather than CPU.
Long agent runs
If a run lasts minutes, save progress after each step. Then a worker restart resumesSession resumption: Continuing an earlier session by reloading its saved history or state. from the last step instead of starting over.
Key points
- Horizontal scaling means adding more identical workers; it only works if any worker can pick up any job.
- Keep conversation and agent state in a shared store, not in a worker's memory.
- Your real ceiling is usually the model rate limit and budget, not CPU.
Move agent state out of workers
Show why horizontal scaling needs state in a shared store, not in a worker's memory.
You will have: A small simulation where a four-step run spread over three workers keeps every fact.
Steps
- Run the starter: both modes lose facts when steps land on different workers.
- Make step() use the shared store when use_shared is True.
- Run again and compare the two lines.
- List what else an agent keeps that must move to the store: history, tool results, step count.
Done when
- Worker memory mode sees only 2 of 4 facts at the final step.
- Shared store mode sees 4 of 4 facts.
- No step depends on which worker ran the previous one.
Open in the Playground No API key needed.
Starter code (Python)
# Stateless workers: keep run state in a shared store so any worker can continue any run.
class Worker:
def __init__(self, name):
self.name, self.local = name, {}
SHARED = {} # stands in for Redis or a database table keyed by run id
FACTS = ["order 1120", "status shipped", "address confirmed", "eta Friday"]
def step(worker, run_id, fact, use_shared):
# TODO: when use_shared is True, keep state in SHARED instead of the worker's own memory
store = worker.local
state = store.setdefault(run_id, {"facts": []})
state["facts"].append(fact)
return len(state["facts"])
def run(use_shared):
SHARED.clear()
workers = [Worker("w1"), Worker("w2"), Worker("w3")]
seen = 0
for i, fact in enumerate(FACTS): # the load balancer sends each step to the next worker
seen = step(workers[i % len(workers)], "run-7", fact, use_shared)
return seen
print(f"worker memory: final step saw {run(False)} of {len(FACTS)} facts")
print(f"shared store: final step saw {run(True)} of {len(FACTS)} facts")
Show a solution
# Stateless workers: keep run state in a shared store so any worker can continue any run.
class Worker:
def __init__(self, name):
self.name, self.local = name, {}
SHARED = {} # stands in for Redis or a database table keyed by run id
FACTS = ["order 1120", "status shipped", "address confirmed", "eta Friday"]
def step(worker, run_id, fact, use_shared):
store = SHARED if use_shared else worker.local
state = store.setdefault(run_id, {"facts": []})
state["facts"].append(fact)
return len(state["facts"])
def run(use_shared):
SHARED.clear()
workers = [Worker("w1"), Worker("w2"), Worker("w3")]
seen = 0
for i, fact in enumerate(FACTS): # the load balancer sends each step to the next worker
seen = step(workers[i % len(workers)], "run-7", fact, use_shared)
return seen
print(f"worker memory: final step saw {run(False)} of {len(FACTS)} facts")
print(f"shared store: final step saw {run(True)} of {len(FACTS)} facts")