Rate limits and concurrency
A rate limit stops one caller from using more than its share of the gateway. Spanway counts three things: requests per minute (RPM), tokens per minute (TPM) and requests running at the same moment, called max parallel requests or concurrency. A concurrency cap is the one that protects slow models best, because a single team firing hundreds of long requests at once can fill every slot even while its per minute numbers look normal.
This page covers where you can set each limit, what a caller sees when it hits one, how to cap single MCP tools, and how the dynamic rate limiter shares a busy model between teams.
A real-world scenario
Two product teams share one deployment of a large reasoning model. The research team runs a batch job that opens 200 requests at once, and the support team's chat assistant starts timing out because every request it sends waits behind the batch. You give the research team max_parallel_requests: 20. The batch job keeps running, 20 requests at a time, and each extra request it sends gets a 429 that says x-spanway-rate-limited-by: team and Retry-After, so the job's client backs off and retries instead of crowding the model. The support team's requests go straight through.
Where each limit lives
| Scope | RPM and TPM | Max parallel requests | Where to set it |
|---|---|---|---|
| Virtual key | rpm_limit, tpm_limit | max_parallel_requests | Key create or edit, or the key's budget |
| User | rpm_limit, tpm_limit | max_parallel_requests | /user/new, /user/update |
| Team | rpm_limit, tpm_limit | max_parallel_requests | Team settings, /team/new, /team/update |
| Team member | rpm_limit, tpm_limit | max_parallel_requests | /team/member_update |
| Customer (end user) | budget rpm_limit, tpm_limit | budget max_parallel_requests | /customer/new, /budget/new |
| Organization | budget rpm_limit, tpm_limit | budget max_parallel_requests | Organizations, /organization/new, /organization/update |
| Project | not counted, only the per model limits below | not counted | /project/new, /project/update |
| Tag | the tag's rpm_limit, tpm_limit | not counted | Tag management |
| Tag on one key | metadata tag_rpm_limit, for example {"batch": 30} | not counted | Key metadata |
| Agent, and one session of an agent | the agent's rpm_limit, tpm_limit, session_rpm_limit, session_tpm_limit | not counted | Agent governance |
| Model on one key | metadata model_rpm_limit, model_tpm_limit, or the rpm_limit and tpm_limit inside a model's entry in the key's model_max_budget when the metadata sets none | metadata model_max_parallel_requests | Key metadata, or Model Max Budget on the key |
| Model on one team, organization or project | metadata model_rpm_limit, model_tpm_limit (projects also model_itpm_limit and model_otpm_limit for input and output tokens) | team metadata model_max_parallel_requests | Team, organization or project metadata |
| MCP tool on one key or team | metadata mcp_tool_rpm_limit | not counted | Key or team metadata |
Every scope that applies to a request is checked, and the request is refused when any one of them is full. A team cap is shared by every key of the team, an organization cap by every key of its teams, a user cap by every key of the user, and a model cap counts only requests for that model, matched by the requested name or its model group. One exception: when a key sets its own model_rpm_limit or model_tpm_limit for a model, that value replaces the team's limit of the same kind for that model, so a key can be given more or less than its team for one model. Use rpm_limit_type: guaranteed_throughput on the key, see below, when key limits must never add up to more than the team has. A project's own rpm_limit and tpm_limit are stored but not enforced, only its per model limits are. A request that is refused never takes a slot, and a slot is given back when the request finishes, fails or the client disconnects. The model maps take a whole number of 0 or more for each model name, and a map in another shape is ignored with a warning in the log.
Choose how strict a key's limits are
A key's rpm_limit_type and tpm_limit_type decide how its limits relate to its team's. In the Admin UI they are the type selectors next to TPM Limit and RPM Limit when you edit a key.
| Value | What it does | When to use it |
|---|---|---|
unset or best_effort_throughput (default) | The key's limits are enforced, and the gateway does not check that the keys of a team add up to no more than the team's limit. | Most keys. |
guaranteed_throughput | Creating or updating the key is refused with 400 when the limits already given to the team's other keys plus this key's limit would be more than the team's limit, so each key really gets what it was promised. | Keys that must have reserved capacity, such as a production service. |
dynamic | The limit is enforced only while the model is failing or answering 429s. When the model is healthy the key may go over its limit and use spare capacity. | Batch or background keys that should soak up idle capacity and step back when the model is under pressure. |
The 400 reads Allocated RPM limit=<sum> + Key RPM limit=<n> is greater than team RPM limit=<team>, or the same with TPM, or with team model specific limits for a model. Lower the key's limit, raise the team's, or use best_effort_throughput.
curl -X POST http://localhost:4000/key/generate \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"team_id": "research", "rpm_limit": 200, "rpm_limit_type": "guaranteed_throughput"}'
Other limit settings, such as which tokens count toward tpm_limit, the window length and default per model limits for keys, are in the settings reference.
Tell the gateway how long answers usually are
A token per minute limit is checked before the model answers, so the gateway has to guess how many tokens the answer will use and sets that many aside (see SPANWAY_TPM_TOKEN_RESERVATION_ENABLED in the settings reference). When your answers are much longer or shorter than the gateway's guess, a busy key either goes over its limit for a moment or gets 429 too early. Give the usual answer length and the guess becomes yours.
| Field | Where | What it does |
|---|---|---|
default_estimated_output_tokens | Body of /key/generate, /key/update, /team/new, /team/update | The answer length, in tokens, the gateway sets aside for every model. A whole number above 0. |
default_estimated_output_tokens_per_model | The same routes | A map such as {"claude-sonnet-5-5": 2000} that overrides the number above for one model. |
The gateway looks in this order and takes the first match: the key's per-model value, the key's value, the team's per-model value, the team's value. With none set it keeps its own guess. A request that sends max_tokens is reserved at that number instead, so these values only matter for requests without it. The values are stored in the key's or team's metadata and only a proxy admin may change them, because a lower number lets a key use more of a team or organization limit than it should. Anyone else who changes them gets 403 Only proxy admins can set default_estimated_output_tokens or default_estimated_output_tokens_per_model on a key (or team). In the Admin UI they show as the estimated output tokens settings on the key and team pages.
Cap a team's parallel requests
-
In the Admin UI, open Teams, select the team and open the Settings tab.
-
Select Edit Settings, enter a number in Max parallel requests, and select Save Changes. Leave the field empty for no cap.
-
Or set it with the API:
curl -X POST http://localhost:4000/team/update \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"team_id": "research", "max_parallel_requests": 20}' -
To cap one member inside a team, send
max_parallel_requeststo/team/member_update:curl -X POST http://localhost:4000/team/member_update \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"team_id": "research", "user_id": "dana", "max_parallel_requests": 4}'Each change of a team's or a member's limits is written to the audit log with the old and new values.
-
To cap one model for the whole team, open Edit Settings again and add a Metadata entry with the key
model_max_parallel_requestsand a value such as{"reasoning-large": 10}, or send the same map inmetadatato/team/update. On a single key, open Virtual Keys, select the key, open the Settings tab, select Edit Settings and put the same entry in the key's Metadata.
Expected result: with 20 requests of the team running, the 21st answers 429 at once, and it is admitted as soon as one of the 20 finishes.
The 429 header contract
Every 429 from the limits on this page, including the dynamic rate limiter, the MCP limits and the batch limits, carries these headers. The one exception is the legacy limiter, turned on with LEGACY_MULTI_INSTANCE_RATE_LIMITING=true, whose 429 carries only Retry-After and x-spanway-rate-limited-by. The session limits (max_iterations and max_budget_per_session) are not counted per minute, so their 429 carries only x-spanway-rate-limited-by, set to the name of the limit:
| Header | Value |
|---|---|
RateLimit-Limit | The limit that was hit, for example 20. |
RateLimit-Remaining | What is left of that limit, 0 when the request was refused. |
RateLimit-Reset | Seconds until it is worth trying again. |
Retry-After | The same number of seconds, for clients that only read this header. |
x-spanway-rate-limited-by | The scope that refused the request, such as api_key, user, team, team_member, end_user, organization, tag, tag_per_key, agent, agent_session, model_per_key, model_per_team, model_per_organization, model_per_project, mcp_server, mcp_per_key, mcp_per_team, mcp_tool_per_key, mcp_tool_per_team, model_saturation_check, priority_model, team_borrow_cap, priority_team_share, batch_enqueued_api_key or batch_enqueued_team. Batch limits use the batch scope, such as api_key_tpd. |
rate_limit_type | requests, tokens or max_parallel_requests. |
A 429 that a model provider sent back is passed through with the provider's own headers instead, so x-spanway-rate-limited-by is how a client tells the gateway's limits apart from the provider's. Header names are case insensitive, and HTTP/2 sends them in lower case.
curl -i http://localhost:4000/v1/chat/completions \
-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \
-d '{"model": "reasoning-large", "messages": [{"role": "user", "content": "Summarise the incident report"}]}'
Expected result when the team is full, other headers left out:
HTTP/1.1 429 Too Many Requests
retry-after: 60
ratelimit-limit: 20
ratelimit-remaining: 0
ratelimit-reset: 60
x-spanway-rate-limited-by: team
rate_limit_type: max_parallel_requests
Cap a single MCP tool
mcp_rpm_limit caps every call to one MCP server. mcp_tool_rpm_limit caps one tool on that server, for example a tool that opens tickets or sends messages, while the rest of the server stays open. The map is keyed by the server name (its alias if it has one) and then by the tool name, and the value is calls per minute.
-
In the Admin UI, open Virtual Keys, select the key, open the Settings tab, select Edit Settings, and in Metadata add
mcp_tool_rpm_limitwith a value such as{"helpdesk": {"create_ticket": 5}}. For a whole team, add the same entry to the team's Metadata under Teams, Settings, Edit Settings. -
Or set it with the API:
curl -X POST http://localhost:4000/key/update \-H "Authorization: Bearer $SPANWAY_API_KEY" -H "Content-Type: application/json" \-d '{"key": "sk-9f3a2c7e41b8d6059012ijkl3456mnop", "metadata": {"mcp_tool_rpm_limit": {"helpdesk": {"create_ticket": 5}}}}'
Expected result: the sixth create_ticket call within a minute answers 429 with x-spanway-rate-limited-by: mcp_tool_per_key, and other tools of the server keep working. A key limit counts that key's calls and a team limit counts the calls of every key of the team. The tool name matches every spelling the gateway routes, with or without the server prefix.
Share a busy model between teams
The dynamic rate limiter (dynamic_rate_limiter_v3) watches how full each model is. While the model is below saturation_threshold, any caller may use idle capacity, which is called borrowing. Once the model is at or above the threshold, each priority class from priority_reservation is held to its reserved share. Two settings make that fairer between teams:
team_borrow_capis the largest share of a model's capacity, from 0 to 1, that one team may use at any time, also while it is borrowing. It is unset by default, which means no team cap. Set it when one team's batch jobs keep taking a busy model from everyone else. A team can carry its own value in its metadata underrate_limit_borrow_cap, which can lower the gateway value but never raise it, so the smaller of the two applies. The share is rounded up and is always at least one request, so a small cap on a small model still lets the team through. A cap of exactly0blocks the team from the model.active_team_splitsplits each priority class's share evenly between the teams that sent it requests in the current minute, so a team that arrives late still gets its part. It is off by default, and off means the share is first come, first served. With Redis configured, the teams are counted across every gateway instance. Without it the share is first come, first served.
spanway_settings:
callbacks: ["dynamic_rate_limiter_v3"]
priority_reservation:
prod: 0.75
dev: 0.25
priority_reservation_settings:
saturation_threshold: 0.8
team_borrow_cap: 0.5
active_team_split: true
The other defaults are saturation_threshold: 0.50, default_priority: 0.25, saturation_check_cache_ttl: 60 seconds, team_borrow_cap unset and active_team_split: false, see the settings reference. The example raises the threshold to 0.8.
- Put the settings above in the config and restart the gateway. Priority reservation needs a valid Enterprise license, not just a value in
SPANWAY_LICENSE. Without one the priority pools are ignored, every key gets thedefault_priorityshare, and the log shows thePREMIUM FEATUREline below. - Give each key or team a priority with
"priority": "prod"in its metadata. Keys without one share one pool, which gets thedefault_priorityshare of the model, 25 percent by default. - To give one team a smaller cap, open Teams, select the team, open the Settings tab, select Edit Settings and add the Metadata entry
rate_limit_borrow_capwith a value such as0.2, or send it inmetadatato/team/update.
Expected result: with a model of 100 requests per minute and team_borrow_cap: 0.5, one team gets at most 50 requests a minute even when nobody else is calling the model, and its 51st answers 429 with x-spanway-rate-limited-by: team_borrow_cap. With active_team_split on and two teams in the prod class during saturation, each team may use half of the prod share, and a team over its half gets x-spanway-rate-limited-by: priority_team_share.
Errors
| Status | Error | Meaning | Fix |
|---|---|---|---|
| 403 | Only proxy admins can set default_estimated_output_tokens or default_estimated_output_tokens_per_model on a <key or team>. ... | A key holder or team admin tried to change the answer length estimate. | Ask a proxy admin to set it. |
| 429 | Rate limit exceeded for <scope>: <id>. Limit type: max_parallel_requests | Too many requests of that scope are running at once. | Wait for running requests to finish, retry after Retry-After, or raise max_parallel_requests for the scope in x-spanway-rate-limited-by. |
| 429 | Rate limit exceeded for <scope>: <id>. Limit type: requests or tokens | The scope used its requests or tokens for this minute. | Retry after RateLimit-Reset seconds, or raise the RPM or TPM limit of that scope. |
| 429 | Rate limit exceeded for mcp_tool_per_key or mcp_tool_per_team | One MCP tool was called more often than mcp_tool_rpm_limit allows. | Slow the agent down or raise the tool's value. |
| 429 | Team borrow cap reached for <model> | The team used its team_borrow_cap share of the model. | Lower the team's load, or raise team_borrow_cap. A team's rate_limit_borrow_cap can only lower the gateway value. |
| 429 | Team share of the <priority> priority pool used up for <model> | During saturation the team used its even share of its priority class. | Retry after Retry-After, add capacity to the model, or turn active_team_split off. |
| 429 | Model capacity reached for <model>. Priority: <priority>, ... | Every caller together used the model's whole TPM or RPM, so x-spanway-rate-limited-by is model_saturation_check. | Add deployments or capacity to the model, or lower saturation_threshold so priority shares start earlier. |
| 429 | Priority-based rate limit exceeded. Model: <model>, Priority: <priority>, ... | The model is busier than saturation_threshold and the caller's priority class used its reserved share, so the scope is priority_model. | Retry after Retry-After, give the key a higher priority, or add capacity. |
| 429 | Rate limit exceeded | The dynamic limiter refused the request for a reason it could not name. | Report it with the ERROR log line Dynamic rate limiter: OVER_LIMIT response with unknown descriptor_key(s). |
| 429 | Crossed TPM / RPM / Max Parallel Request Limit | Only with the legacy limiter (LEGACY_MULTI_INSTANCE_RATE_LIMITING=true). | Raise the limit, or turn the legacy limiter off. |
| 400 | Allocated RPM limit=<sum> + Key RPM limit=<n> is greater than team RPM limit=<team> | The key uses guaranteed_throughput and the team has no capacity left to promise. The same message names TPM, or a model, for those limits. | Lower the key's limit, raise the team's, or use best_effort_throughput. |
| 503 | Rate limit enforcement unavailable: request counters could not be verified against Redis | general_settings.fail_closed_rate_limit_enforcement is on (the default) and Redis could not be reached, so no limit could be checked. | Restore Redis. Setting fail_closed_rate_limit_enforcement: false lets requests through on the local counters instead. |
Logs
| Level | Line | Meaning |
|---|---|---|
| WARNING | Ignoring metadata.model_max_parallel_requests: expected a map of name to a whole number of 0 or more | A key or team carries a model concurrency map in the wrong shape, so no model cap applies. Fix the metadata. The same line names model_rpm_limit or model_tpm_limit when one of those maps, on a key, team, organization or project, is in the wrong shape. |
| WARNING | Ignoring metadata.mcp_tool_rpm_limit: expected a map of server name to a map of tool name to a whole number | The MCP tool limits are in the wrong shape and are not applied. Fix the metadata. |
| WARNING | Ignoring team metadata.rate_limit_borrow_cap: expected a fraction from 0 to 1 | The team's borrow cap is not a number from 0 to 1, so the gateway team_borrow_cap applies instead. Fix the metadata. |
| WARNING | team activity script failed, using local view: <error> | Redis could not count the active teams for active_team_split, so this instance splits the pool between the teams it has seen itself until Redis is back. Check Redis. |
| ERROR | Dynamic rate limiter: OVER_LIMIT response with unknown descriptor_key(s) | The dynamic limiter refused a request for a reason it could not name. The request still gets a 429. Report it with the log line. |
| ERROR | PREMIUM FEATURE: Reserving tpm/rpm by priority is a premium feature. Please add a valid 'SPANWAY_LICENSE' ... | priority_reservation is set without a valid license, so the priority pools are ignored and every key uses default_priority. Add or renew the license. |
Related
- Budgets for spend limits and the budget fields that carry RPM, TPM and concurrency.
- Team budgets for team and team member limits.
- Customers for end user budgets.
- Virtual keys for key level limits and metadata.
- Redis requirements for sharing limits across gateway instances.
- Audit logs for the rows a team or member limit change writes.
- Error reference for every 429 the gateway returns.