Skip to main content

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​

ScopeRPM and TPMMax parallel requestsWhere to set it
Virtual keyrpm_limit, tpm_limitmax_parallel_requestsKey create or edit, or the key's budget
Userrpm_limit, tpm_limitmax_parallel_requests/user/new, /user/update
Teamrpm_limit, tpm_limitmax_parallel_requestsTeam settings, /team/new, /team/update
Team memberrpm_limit, tpm_limitmax_parallel_requests/team/member_update
Customer (end user)budget rpm_limit, tpm_limitbudget max_parallel_requests/customer/new, /budget/new
Organizationbudget rpm_limit, tpm_limitbudget max_parallel_requestsOrganizations, /organization/new, /organization/update
Projectnot counted, only the per model limits belownot counted/project/new, /project/update
Tagthe tag's rpm_limit, tpm_limitnot countedTag management
Tag on one keymetadata tag_rpm_limit, for example {"batch": 30}not countedKey metadata
Agent, and one session of an agentthe agent's rpm_limit, tpm_limit, session_rpm_limit, session_tpm_limitnot countedAgent governance
Model on one keymetadata 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 nonemetadata model_max_parallel_requestsKey metadata, or Model Max Budget on the key
Model on one team, organization or projectmetadata 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_requestsTeam, organization or project metadata
MCP tool on one key or teammetadata mcp_tool_rpm_limitnot countedKey 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.

ValueWhat it doesWhen 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_throughputCreating 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.
dynamicThe 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.

FieldWhereWhat it does
default_estimated_output_tokensBody of /key/generate, /key/update, /team/new, /team/updateThe answer length, in tokens, the gateway sets aside for every model. A whole number above 0.
default_estimated_output_tokens_per_modelThe same routesA 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​

  1. In the Admin UI, open Teams, select the team and open the Settings tab.

  2. Select Edit Settings, enter a number in Max parallel requests, and select Save Changes. Leave the field empty for no cap.

  3. 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}'
  4. To cap one member inside a team, send max_parallel_requests to /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.

  5. To cap one model for the whole team, open Edit Settings again and add a Metadata entry with the key model_max_parallel_requests and a value such as {"reasoning-large": 10}, or send the same map in metadata to /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:

HeaderValue
RateLimit-LimitThe limit that was hit, for example 20.
RateLimit-RemainingWhat is left of that limit, 0 when the request was refused.
RateLimit-ResetSeconds until it is worth trying again.
Retry-AfterThe same number of seconds, for clients that only read this header.
x-spanway-rate-limited-byThe 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_typerequests, 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.

  1. In the Admin UI, open Virtual Keys, select the key, open the Settings tab, select Edit Settings, and in Metadata add mcp_tool_rpm_limit with 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.

  2. 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_cap is 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 under rate_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 exactly 0 blocks the team from the model.
  • active_team_split splits 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.

  1. 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 the default_priority share, and the log shows the PREMIUM FEATURE line below.
  2. Give each key or team a priority with "priority": "prod" in its metadata. Keys without one share one pool, which gets the default_priority share of the model, 25 percent by default.
  3. 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_cap with a value such as 0.2, or send it in metadata to /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​

StatusErrorMeaningFix
403Only 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.
429Rate limit exceeded for <scope>: <id>. Limit type: max_parallel_requestsToo 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.
429Rate limit exceeded for <scope>: <id>. Limit type: requests or tokensThe scope used its requests or tokens for this minute.Retry after RateLimit-Reset seconds, or raise the RPM or TPM limit of that scope.
429Rate limit exceeded for mcp_tool_per_key or mcp_tool_per_teamOne MCP tool was called more often than mcp_tool_rpm_limit allows.Slow the agent down or raise the tool's value.
429Team 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.
429Team 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.
429Model 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.
429Priority-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.
429Rate limit exceededThe 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).
429Crossed TPM / RPM / Max Parallel Request LimitOnly with the legacy limiter (LEGACY_MULTI_INSTANCE_RATE_LIMITING=true).Raise the limit, or turn the legacy limiter off.
400Allocated 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.
503Rate limit enforcement unavailable: request counters could not be verified against Redisgeneral_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​

LevelLineMeaning
WARNINGIgnoring metadata.model_max_parallel_requests: expected a map of name to a whole number of 0 or moreA 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.
WARNINGIgnoring metadata.mcp_tool_rpm_limit: expected a map of server name to a map of tool name to a whole numberThe MCP tool limits are in the wrong shape and are not applied. Fix the metadata.
WARNINGIgnoring team metadata.rate_limit_borrow_cap: expected a fraction from 0 to 1The team's borrow cap is not a number from 0 to 1, so the gateway team_borrow_cap applies instead. Fix the metadata.
WARNINGteam 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.
ERRORDynamic 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.
ERRORPREMIUM 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.