Your requests-per-minute limiter won't stop one customer from starting three hundred agent runs in the same second. One line of queueing math sizes a cap on simultaneous runs, and a per-tenant share keeps the loudest customer from starving everyone else.
- A requests-per-minute limiter controls how fast calls arrive but never how many agent runs are alive at once, which is the number that actually exhausts shared quota.
- Little's Law gives the cap directly: concurrent runs equal the run rate your provider limit allows, multiplied by how long a run lasts.
- Work the sum for requests per minute and for tokens per minute, then take the smaller result, because the token limit usually binds first for agents that resend a growing history.
- Size the cap from your fast runs, not your average, because short runs finish early and push the arrival rate up.
- Admit whole runs rather than single calls, and give each tenant a hard share of the global cap so a burst from one customer queues behind everyone else's work.
Most teams ask how to set concurrency limit for AI agents after the incident, not before it. The setup is familiar. A rate limiter sits in front of the model API, set to a comfortable number of requests per minute, and everything looks under control until one customer uploads 300 documents and each upload starts a twelve-step agent run. Nothing in that limiter says how many runs may be alive at once. So 300 runs start together, every one fires its first call inside the same second, the provider starts answering with 429s, and the retry logic makes it worse.
The 429 itself is the cheap part. The expensive part is what happens to the run underneath it.
An agent run isn't one request. It's a loop of model calls and tool calls, and each call resends the growing history, so the late steps are the biggest ones. If a run fails at step nine and the worker restarts it from step one, you pay for steps one to eight twice. Multiply that by a few hundred runs and the invoice jumps while the customer sees nothing but errors. Anthropic documents its limits as requests per minute, input tokens per minute and output tokens per minute, enforced as a token bucket, and returns a 429 with a retry-after header when you cross one. OpenAI measures requests and tokens per minute too. Both companies' Python SDKs retry failed requests twice by default, which means a burst of rejected calls quietly turns into a bigger burst of retried ones.
A per-minute limiter governs arrivals. It tells you how fast new calls may enter the system. It says nothing about work already in progress, and for agents work in progress is the thing that hurts. A run that takes 45 seconds holds a worker, a database connection, a slice of memory and a share of your quota for the whole 45 seconds. Start 300 of them together and you've committed 300 slots of everything, whatever the limiter allowed in the first second.
How to Set Token Budget Per Task for AI Agents Before Costs Spiral A 200,000-token budget can be split into six slices, with 35% or 70,000 tokens going to tool output. Capping results at about 3,000 tokens turns a 220,000-token bill for one page carried through 11 calls into 33,000 tokens. - how to set AI agent token budgets - prevent runaway AI agent token costs
Queueing theory has a plain answer for this, and it's old. John Little proved in 1961 that for any stable system, the average number of items inside it equals the average arrival rate times the average time each item spends there. Written short, L = λW. L is the number of runs in flight, λ is runs started per second, and W is seconds per run. You don't need a model of your traffic or any assumption about how requests are distributed. It holds for the averages whatever the pattern underneath.
Flip it around and it becomes a design tool. If the provider lets you sustain a certain run rate, then the number of runs you can keep in flight is that rate multiplied by the run duration. Cap the in-flight number and you've capped the rate, because a run can't start until another one finishes.
Sizing the cap from your own numbers #
Here's a worked example. The figures are illustrative, so swap in yours, but the method is the same everywhere. Say your provider tier allows 1,000 requests per minute and 2,000,000 input tokens per minute. Your agent makes 12 model calls per run and sends about 6,000 input tokens on each call, which is 72,000 input tokens per run. The average run lasts 45 seconds from start to final answer.
Start with requests. Keep 20% in reserve, so you plan around 800 requests per minute. Divide by 12 calls and you get about 67 runs per minute, or 1.11 runs per second. Multiply by 45 seconds and the request limit allows roughly 50 runs in flight.
Now tokens, which is the check people skip. Two million input tokens per minute divided by 72,000 tokens per run is about 27.8 runs per minute, or 0.46 per second. Multiply by 45 seconds and you get 21 runs in flight at full load, or about 16 with the same 20% reserve. The token limit is three times tighter than the request limit. That's normal for agents, because they resend context on every step, and it's why a cap built from requests per minute alone so often fails in production. Work the sum for every limit your provider publishes, including output tokens, and keep the smallest answer.
Why the reserve? Because the key rarely belongs to one workload. Your evals, your internal tools and a developer testing a prompt on Friday afternoon all draw from the same bucket. Do the sum on what's left after them, not on the number in the pricing table.
Then break your own average. This is where most sizing exercises go wrong, because Little's Law runs on averages and your burst won't be average. Short runs, such as cache hits, easy questions or an upstream tool that answered fast, finish early and free their slot early. A cap of 16 with 45-second runs holds the system near 0.36 runs per second. If a burst of easy work drops the duration to 15 seconds, the same 16 slots turn over at more than one run per second, about 64 runs a minute. That's 4.6 million input tokens a minute against a limit of 2 million. The cap held. The rate didn't.
How to Manage LLM Context Window Budget Before Long Prompts Hurt Quality Chroma tested 18 models, including GPT-4.1, Claude Opus 4 and Gemini 2.5, and found performance dropped as input length grew even on simple tasks. Nvidia's RULER benchmark found only about half the models claiming 32K tokens or more held up at 32K. - how to manage LLM context window budget - prevent long prompts from hurting LLM quality
So size from the fast end. Pull the 10th or 25th percentile of run duration from your logs, not the mean. With a 20-second fast duration, the token-bound sum gives 0.46 runs per second times 20 seconds times 0.8, which is a cap of about seven. Seven is a long way from 50, and that gap is exactly the gap between a system that survives a burst and one that doesn't. You can always raise a cap later with evidence. You can't un-spend a bill.
To get the inputs, log four things per run: start time, end time, number of model calls and total input tokens. Four columns and a day of production traffic give you every number in the example above.
Enforce the agent worker queue concurrency cap on whole runs #
The next decision is what the cap counts, and I'd argue it should count runs, not individual calls. Gating each call looks tidier, since the provider limit is about calls. But it breeds a nasty failure. Fifty runs each hold a half-finished state and fight for call slots, so none of them can finish, and every one of them sits in memory costing you money while it waits. Admit a run once, give it a slot for its whole life, and release the slot when it ends. A run that has started is guaranteed to be able to finish, and everything not yet admitted waits in a queue where it costs nothing.
The tools you probably already run can do this. In Celery, the worker_concurrency setting fixes how many tasks a worker runs at once, and for long tasks the Celery docs recommend setting the prefetch multiplier to 1 with late acknowledgement, so a worker doesn't grab a pile of queued runs it can't start. Temporal workers expose a maximum for concurrent activity executions, and a limit on concurrent workflow tasks, that do the same job. If you run several worker processes against one provider key, the per-worker numbers have to add up to your global cap, or you need a shared counter. A Redis semaphore with an expiry on each slot works, as long as the expiry is longer than your slowest run and a crashed worker's slot clears itself.
Give the queue a ceiling on wait time too. If a run has been queued for ten minutes, the user has already left. Reject it at admission with your own 429 and a Retry-After header, so the caller knows to back off, instead of letting an invisible backlog grow. Cap the retries inside a run as well, say three attempts per call with exponential backoff and jitter, honoring the provider's retry-after value when it sends one. Retries should draw from the same budget as first attempts. Otherwise a rate-limited run burns slots and tokens retrying while healthy runs wait behind it.
How to limit concurrent LLM requests per tenant #
A global cap protects the provider quota. It does nothing about who gets the slots. With one first-in, first-out queue, the customer who submits 300 runs at 9:00 owns the front of the line, and the customer who submits one run at 9:01 waits behind all of them. To that second customer, your product is down. The cap did its job and the service still failed.
Fairness needs two pieces. The first is a per-tenant ceiling. With a global cap of seven, give any single tenant at most three, roughly 40%. Two or three active tenants can then always make progress, and the loud one sees its own backlog grow instead of everyone else's. The second piece is how the next slot gets handed out. When a slot frees up, don't take the oldest job in the queue. Take the oldest job from the tenant with the fewest runs currently in flight, or rotate through tenants that have work waiting. That's weighted round-robin, and it's a few dozen lines on top of a queue per tenant.
There's a real argument for letting a tenant exceed its share when nobody else is using the capacity. It keeps your hardware busy and makes the system feel faster at quiet times. I wouldn't do it for agents. You can't take a slot back from a run that is forty seconds into a multi-step plan without wasting its spend, so a tenant that borrows idle capacity can still be holding it when the next customer shows up. Keep the per-tenant ceiling hard, and set it by plan tier if you sell tiers, so a customer paying for more parallelism gets four slots and the free tier gets one.
Spend deserves its own fuse, because concurrency caps limit the rate of spending, not the total. Put a hard call budget on every run, 25 model calls for a workflow that normally takes 12, so a loop that never terminates gets killed instead of billed. Add a daily token or dollar ceiling per tenant that s admission when it's hit and alerts you. A concurrency cap bounds how fast the money goes out. These two bound how much.
What to watch after you ship it #
Track five numbers: slots in use against the cap, queue wait at the 95th percentile, 429s returned by the provider, 429s you returned to your own callers, and each tenant's share of completed runs. The provider 429 count is the one that grades your math. After the change it should sit near zero. If it doesn't, your duration percentile was too optimistic or something else shares the key, and the fix is in the inputs, not in the formula.
Raise the cap in small steps and only when queue wait is hurting and provider 429s are at zero. A jump from seven to ten is a test you can read in an afternoon. A jump from seven to fifty is the incident you started out trying to avoid. Pull the 10th percentile run duration from last week's logs tonight, since that single number moves the answer more than any other input.
Also read: What Are RSA and Elliptic Curve Cryptography, and Why Moving to Post-Quantum Cryptography Matters Now • Outcome based pricing for AI agents without losing margin • How to Set an AI Agent Human Approval Threshold That Reviewers Respect
This article is posted in AI News, check it out for more related stories.
Join the discussion #
Open in the community → Almost there. Sign in and your reply posts straight away.