Batching completions
Sometimes you have a list of prompts to run, not a conversation. Evaluations, nightly summaries and model comparisons all send many independent requests and want them done quickly. Through the gateway you do this with concurrent calls to /v1/chat/completions, and the gateway logs, costs and rate limits every one of them against the key. For asynchronous batch processing through a provider's batch API at a lower price, see batches.
Scenario
A support team summarises every ticket closed during the day. At midnight a job sends two hundred summary requests at once with a key dedicated to the job. The key has a parallel request limit that keeps the job from starving interactive users, a budget that caps what the job can spend, and the Usage page shows the job's cost as its own line.
Steps
-
Create a key for the job. In the Admin UI open Virtual Keys (
/ui/api-keys), click + Create New Key, give it a Key Name such asticket-summaries, pick the model under Models, open Optional Settings, set Max Budget (USD) and click Create Key. Then click the new key in the list, open its Settings tab, click Edit Settings, set Max Parallel Requests and save. The create dialog has no parallel request field. -
Send the requests concurrently. The example sends three summaries at once from the shell.
for ticket in 101 102 103; docurl -s -X POST http://localhost:4000/v1/chat/completions \-H "Authorization: Bearer $SPANWAY_API_KEY" \-H "Content-Type: application/json" \-d "{\"model\": \"claude-haiku-5-5\", \"messages\": [{\"role\": \"user\", \"content\": \"Summarize ticket $ticket\"}], \"max_tokens\": 200}" &donewaitIn Python, use
asyncio.gatheroverclient.chat.completions.createcalls of the OpenAI SDK pointed at the gateway, with a semaphore sized to the key's parallel request limit. -
Compare models on the same prompt. Open Playground (
/ui/playground), switch to the Compare tab, pick two or three models and send one prompt to see the answers side by side. -
Check the job's spend. Open Usage (
/ui/usage) and filter by the key, or open Logs (/ui/logs) and filter the Request Logs tab by the key to see every call with its cost.
Expected result
Each request returns its own chat completion. Requests beyond the key's Max Parallel Requests are answered with 429 until earlier ones finish, and the Usage page shows the total cost of the run under the key. A failed request does not affect the others.
Sizing the key
Rate limits on the key apply to the whole run, so size the key's rpm_limit, tpm_limit and max_parallel_requests for your concurrency. Provider rate limits still apply behind the gateway, and the router spreads the load across deployments and retries or falls back when a provider answers 429.
Errors
| Status | What it means | Fix |
|---|---|---|
429 RateLimitError with max_parallel_requests | More requests are in flight than the key allows. | Lower the concurrency in the job or raise Max Parallel Requests on the key. |
429 RateLimitError with rpm_limit or tpm_limit | The key's requests or tokens per minute are exhausted. | Spread the job over more minutes or raise the limit. |
| 429 from the provider | The provider is throttling. | Add more deployments to the model group so the router can spread the load, and keep retries on. |
422 budget_exceeded (BudgetExceededError) | The key spent its budget. The message starts with Budget has been exceeded!. | Raise Max Budget or wait for the budget reset. spanway_settings.budget_exceeded_status_code: 429 returns 429 instead, see the error reference. |
Logs
Concurrent calls are ordinary requests, so they write the same lines as any chat completion, see the log reference. When a key's parallel limit refuses a call, the rate limit lines there name the key and the limit. Nothing is logged for a job as a whole.
Related
- Batches for provider batch APIs with a 24 hour window and lower prices.
- Virtual keys for limits and budgets on keys.
- Reliability for retries and fallbacks.
- Playground page for the Compare tab.