Skip to main content

Gateway overview

The Spanway gateway is an HTTP server that sits between your applications and model providers. Applications send OpenAI-format requests to one base URL, and the gateway decides which provider deployment serves each request, translates the request to that provider's format, and translates the response back. Because the API surface is the same for every provider, switching a team from one model to another is a config change, not a code change.

A real-world scenario​

An internal tools team ships a document summariser used by four departments. It was written against one provider's SDK. The platform team registers that model and a second provider's model under the same model_name on the gateway, hands the tools team one virtual key with a monthly budget, and points the application's base URL at the gateway. The application code does not change. When one provider has an outage the router sends traffic to the other, when the budget runs out the key is refused instead of the bill growing, and the finance team reads the department's spend from the Admin UI.

How a request flows​

  1. The client sends a request with a virtual key and a public model name.
  2. The gateway authenticates the key and checks its model access, budget and rate limits, along with those of its team, user and organization.
  3. Pre-call guardrails run, and the cache is checked if caching is on.
  4. The router picks a deployment in the model group using the routing strategy, skipping deployments in cooldown.
  5. The provider is called. Failures are retried and, if configured, fallbacks are tried.
  6. Post-call guardrails run, cost is calculated, and the request is logged to the database and to logging integrations.

Model groups and load balancing​

Several model_list entries with the same model_name form a model group. The router spreads traffic across them, which lets you combine regions, accounts or providers behind one name.

model_list:
- model_name: claude-sonnet-5-5
spanway_params:
model: anthropic/claude-sonnet-5-5
api_key: os.environ/ANTHROPIC_API_KEY
rpm: 500
- model_name: claude-sonnet-5-5
spanway_params:
model: bedrock/us.anthropic.claude-sonnet-5-5
aws_region_name: us-east-1
rpm: 1000

See routing for strategies, retries and fallbacks.

Supported endpoints​

The gateway serves chat completions, the Responses API, Anthropic Messages, embeddings, images, audio, rerank, moderation, OCR, video, batches, files, fine-tuning, vector stores, search, MCP and A2A agents, plus native pass-through routes for each major provider. The full list is on supported endpoints.

Managing auth - virtual keys​

The gateway has two kinds of credentials. The master key, set with general_settings.master_key or SPANWAY_MASTER_KEY, is the admin credential. It can call every route and create other credentials. The gateway refuses to start when the master key is unset, empty, or a publicly known example value. Virtual keys are what applications use. Each one carries its own model allowlist, budget, rate limits and metadata, and every request it makes is costed and logged against it.

curl -X POST http://localhost:4000/key/generate \
-H "Authorization: Bearer $SPANWAY_API_KEY" \
-H "Content-Type: application/json" \
-d '{"models": ["claude-sonnet-5-5"], "max_budget": 10, "duration": "30d"}'

The response contains the new key. Give it to the application and keep the master key private. Virtual keys need Postgres, configured with DATABASE_URL. Read virtual keys for every option, and users and teams for grouping keys under people and teams.

Step by step​

  1. Register two deployments under one model_name as shown above and start the gateway.

  2. Create a virtual key for the application. In the Admin UI open Virtual Keys, press + Create New Key, choose the model and a budget, and copy the key. The API form is the /key/generate call above.

    Expected result:

    {"key": "sk-...", "models": ["claude-sonnet-5-5"], "max_budget": 10.0, "expires": "2026-12-21T10:00:00Z"}
  3. Point the application at the gateway and send a request with the new key:

    curl http://localhost:4000/v1/chat/completions \
    -H "Authorization: Bearer sk-the-new-key" -H "Content-Type: application/json" \
    -d '{"model": "claude-sonnet-5-5", "messages": [{"role": "user", "content": "Summarise this paragraph: ..."}]}'

    Expected result: a chat completion, and the x-spanway-model-id header names which of the two deployments served it.

  4. Open Logs in the Admin UI to see the request with its deployment, tokens and cost, and Usage to see the key's spend against its budget.

Errors​

StatusMeaningFix
401 Authentication Error, No api key passed in.The request carried no key or an invalid one.Send Authorization: Bearer with a virtual key or the master key.
401 Authentication Error, Invalid proxy server token passed.The key is unknown, deleted or blocked.Create a new key, or unblock it in Virtual Keys.
400 Budget has been exceededThe key, team or user spent its budget.Raise the budget or wait for the reset.
401 key not allowed to access modelThe key's model allow-list does not include the model.Add the model to the key or team.
429 Max parallel request limit reached or Rate limit reachedThe key or team is over its rpm, tpm or parallel request limit.Raise the limit on the key or team, or spread the load.
503 No deployments available for selected modelEvery deployment of the model group is in cooldown or unhealthy.Check Models + Endpoints, Health Status, and the provider status.
500 No connected database on /key/generateVirtual keys need Postgres.Set DATABASE_URL.