JustujuAI, layer by layer.Open in the interactive map

Model APIs & routers

Paying per token to send requests to a hosted model, either directly to a provider or through a router that picks among many providers.

Layer 6 · Inference & Optimization · Run time · reviewed 2026-10-30

Read first: Tokens & tokenization.

SimpleA plain explanation, one diagram and an analogy, with where the analogy breaks.

Guess first: what does the router do when one provider is down?

Show the answerIt picks another company that is working, like a taxi app sending a car from a different firm.

Most people never run an AI themselves. Their app sends a message over the internet to a company that runs it, and pays a tiny amount for each word.

A router is like a taxi app. You ask for a ride, and it picks which company sends the car.

A model API is a web endpoint: you send the conversation as JSON and get the reply back, often streamed token by token. You pay per token, usually quoted per million, and input and output tokens are often priced differently.

Many providers and local servers copy the same request format, often called OpenAI-compatible. Switching can be as small as changing the base URL and the model name.

A router or aggregator sits in front of many providers. One key and one bill reach many models, and it can send each request to whichever provider is cheaper, faster or currently up.

Your appRouterProviderrequest: model name + messagesforward to a chosen providerstream tokens + usage countsstream reply, bill per token
One request through a router

Like: A taxi app that picks which company sends the car.
Where it breaks: Any taxi gets you to the same place. Two providers serving the "same" model can differ in quantization, context limit and supported features, so the answer you get may depend on which one the router picked.

How it worksAn interactive demo first, then the mechanism: light maths, pseudo-code and trade-offs.

A request's bill is tokens × price, summed over its parts. Input and output are priced separately per million tokens, and output usually costs several times more per token than input.

Prompt caching stores a repeated prefix, such as instructions, tool definitions or a long document. A request that finds it cached pays a fraction of the input price for those tokens; one that misses pays a premium to write it. Only an exact prefix matches, so anything that changes must go at the end.

A batch interface trades speed for price: you submit many requests at once and collect the results within hours, at a discount. Agents multiply everything, because one task means many calls, each resending its context.

Try the interactive version in the map.

Things to try

# rates in $ per million tokens (illustrative)
tokens_in  = input_tokens  * workload   # chat 1, agent ~4, multi-agent ~15
tokens_out = output_tokens * workload
prefix = tokens_in * cached_share
fresh  = tokens_in - prefix
hits   = prefix * hit_rate
misses = prefix - hits
discount = 0.5 if batch else 1
cost = discount * (fresh  * in_price
                 + hits   * in_price * read_x     # e.g. 0.1
                 + misses * in_price * write_x    # e.g. 1.25
                 + tokens_out * out_price) / 1_000_000
per_day   = cost * requests_per_day
per_month = per_day * 30
# caching beats no caching when hit_rate * read_x + (1 - hit_rate) * write_x < 1

Trade-offs

ResearchWhere the simple picture breaks, open problems and primary sources.

One request format makes providers look interchangeable. In practice the same call can behave differently by provider, by date and even by server load.

Compatibility is a convention. Tool calling, caching, reasoning settings and errors differ between "compatible" servers, so a switch of provider needs its own tests (one compatible server).

Same name, different model. A hosted model can change behind a fixed name: one study saw accuracy on a maths task fall from 84% to 51% between two releases three months apart (Chen et al., 2023). Pin versions and rerun evals.

Not deterministic at temperature 0. Server kernels change their order of arithmetic with batch size, which depends on other users' traffic, so identical requests can get different answers (Thinking Machines, 2025).

Hidden tokens. Internal reasoning is billed as output tokens even when the app shows little of it, so the visible answer understates the cost (one provider’s docs).

Security on this concept: 3 threats Pending expert review

Encrypted chat traffic still reveals the topic Pending expert review

Exposed
  • Streamed responses, sent token by token over encrypted connections
  • The sizes and timing of those packets, visible to anyone on the network path
Attacks
  • Training a classifier on packet sizes and timing to guess what a conversation is about
Real case
Research demonstration: Whisper Leak (2025-11)
Controls
  • Pad streamed chunks with random-length filler
  • Batch several tokens per packet
  • Offer a non-streaming mode for sensitive use
Owner
Platform/Cloud, with ML engineering
Reference
None verified

Copying a model by harvesting its answers Pending expert review

Exposed
  • A model's capabilities, reachable through its public API or app
  • Account sign-up and usage limits
Attacks
  • Running many fake accounts to collect answers at scale
  • Training a cheaper model on the collected answers (distillation)
Real case
Incident: Distillation campaigns against a frontier model provider (2026-02)
Controls
  • Detect coordinated accounts and unusual query patterns
  • Rate limits and identity checks matched to the risk of each tier
  • Terms of use that forbid it, with enforcement
Owner
CISO, with Legal/Compliance
Reference
AML.T0005 Replicate AI Model Behavior
AML.T0048.004 AI Intellectual Property Theft

Stolen cloud keys used to run someone else's AI bill Pending expert review

Exposed
  • Cloud accounts with access to hosted model services
  • API keys in code, CI systems and laptops
Attacks
  • Using leaked keys to call expensive models (LLMjacking)
  • Reselling access to the stolen model quota
Real case
Incident: LLMjacking (2024-05)
Controls
  • Least-privilege roles: only the accounts that need model access get it
  • Short-lived credentials instead of long-lived keys; secret scanning in repositories
  • Spending limits and alerts on model usage
Owner
Platform/Cloud, with CISO
Reference
LLM06:2026 Unbounded Consumption
AML.T0012 Valid Accounts
AML.T0034 Cost Harvesting

All layers in the security view

Examples as of 2026-10-30

Sources

  1. OpenAI API reference: Chat Completions (2023)
  2. OpenRouter documentation: Quickstart (2024)
  3. vLLM documentation: OpenAI-Compatible Server (2025)
  4. Pricing: prompt caching and batch processing (Claude API documentation) (2026-10-15)
  5. Prompt caching (Claude API documentation) (2026-10-15)
  6. Batch API guide (OpenAI API documentation) (2026-10-15)
  7. Prompt caching guide (OpenAI API documentation) (2026-10-15)
  8. How we built our multi-agent research system (Anthropic) (2025-06-13)
  9. How is ChatGPT's behavior changing over time? (Chen, Zaharia, Zou) (2023-07-18)
  10. Defeating Nondeterminism in LLM Inference (Thinking Machines Lab) (2025-09-10)
  11. Extended thinking (Claude API documentation) (2026)

How it connects

More in Layer 6 · Inference & Optimization