AI gateways

An AI gateway sits between your app and the model provider. It routes calls, caches answers, limits spend and keeps its own log. Heystack is never a gateway or a proxy: your calls still go straight from your app, or from your own gateway, to the provider. This page covers how Heystack reads what three common gateways record:

  • LiteLLM;
  • Cloudflare AI Gateway;
  • Vercel AI Gateway.

Being rolled out. Reading gateway records is switching on for organisations in stages. The Cloudflare AI Gateway log endpoint (/v1/gateway-logs/cloudflare) is open; its OpenTelemetry export is not yet confirmed against a live gateway.

What Heystack reads

GatewayHow it reaches HeystackWhat appears
LiteLLM (your proxy or the library)Its otel callback sends OpenTelemetry to https://ingest.heystack.dev/v1/tracesCalls, the provider's model, tokens, the cost LiteLLM reports, errors, latency, text, and the consumer: the LiteLLM team, else the key alias, else the end user
Cloudflare AI Gateway, called from a Worker that uses @heystack/otel/workersAutomaticCalls, model, tokens, cost and errors, under the real provider (OpenAI, Anthropic and so on). The provider is read from the gateway URL.
Cloudflare AI Gateway, its own logs (Logpush)Logpush to https://ingest.heystack.dev/v1/gateway-logs/cloudflareCalls, provider, model, the cost Cloudflare reports, status, cached and rate-limited calls, latency, and the consumer from heystack_consumer metadata, or else the gateway name. No tokens and no text: Logpush doesn't include tokens, and it encrypts request and response bodies. Heystack never stores the bodies.
Cloudflare AI Gateway, its OpenTelemetry exportOpenTelemetry to https://ingest.heystack.dev/v1/tracesCalls, model, tokens, cost and text. Not confirmed against a live gateway yet.
Vercel AI Gateway (the AI SDK gateway provider)Your app's AI SDK telemetry; see AI frameworksAs for the AI SDK, with the real provider taken from the model id (openai/gpt-4o-mini → OpenAI) and the cost the gateway reports

Each gateway record appears in AI & Safety like any other AI call, labelled with its gateway and consumer.

LiteLLM

Turn on LiteLLM's otel callback and point it at Heystack with a server key.

In the proxy's config.yaml:

litellm_settings:
  callbacks: ["otel"]

Then set these in the proxy's environment:

OTEL_EXPORTER="otlp_http"
OTEL_ENDPOINT="https://ingest.heystack.dev/v1/traces"
OTEL_HEADERS="Authorization=Bearer sk_live_…"
OTEL_SERVICE_NAME="litellm-gateway"

With the library, set litellm.callbacks = ["otel"] and use the same environment variables. Create the key under Settings › App › Connection & keys for the app that should own the gateway's calls (see Spend: counting each call once).

Heystack keeps these from LiteLLM's records:

  • The consumer label: the team alias, else the virtual key's alias, else the end user. Use teams or key aliases to tell your apps or customers apart.
  • Each call's operation (acompletion shows as a chat call) and the provider model (for example gpt-4o-mini). When the LiteLLM model group has a different name, it is kept too.
  • The LiteLLM key hash, shortened to a fingerprint. The key itself is never sent.

Cloudflare AI Gateway

From a Worker

If your Worker calls the gateway URL (https://gateway.ai.cloudflare.com/v1/<account>/<gateway>/<provider>/…) and uses @heystack/otel/workers, there is nothing to set up. The provider comes from the <provider> part of the URL, so a call through the gateway to OpenAI shows as OpenAI. With the compat endpoint, the provider comes from the model name.

Logpush

Use Logpush when calls go through the gateway from somewhere that doesn't send to Heystack, or when you want the gateway's own record.

  1. In the Cloudflare dashboard, create a Logpush job for your AI Gateway's events, with an HTTP destination:

    https://ingest.heystack.dev/v1/gateway-logs/cloudflare?header_Authorization=Bearer%20sk_live_…
    

    Use a server key. A browser key (hs_web_…) is refused.

  2. Keep each upload within 5,000 records and 5 MB. Larger uploads are refused with 413. Gzip is accepted.

  3. To name the caller, send a cf-aig-metadata header with {"heystack_consumer": "<name>"} on each gateway request. Without it, the consumer is the gateway's name.

What a Logpush record gives and doesn't give:

  • Given: provider, requested and served model, the cost Cloudflare reports, status, whether the answer came from cache, whether the call was rate limited, and latency. A call with a status of 400 or above, or one that was rate limited, counts as failed.
  • Not given: tokens, because the record has none, and text, because Cloudflare encrypts the bodies.

Re-sent files are merged. A Logpush record has no id, so Heystack makes one from the record itself: the gateway, time, endpoint, provider, requested and served model, status, latency, cost, cache and rate-limit flags, and routing decision. If Cloudflare sends the same file twice, each record keeps the same id and is stored once. Two different calls that match on every one of those fields would also be stored once. That needs the same millisecond, latency and cost.

Vercel AI Gateway

Use the AI SDK as in AI frameworks with the gateway provider (gateway("openai/gpt-4o-mini"), or a plain "openai/gpt-4o-mini" model string). Heystack takes the provider from the model id. To see the gateway's reported cost, turn on provider metadata in the AI SDK's OpenTelemetry integration (new OpenTelemetry({ providerMetadata: true })).

Spend: counting each call once

A gateway's record and your app's own record can describe the same call. Heystack does not join the two one by one, so:

  • Send a gateway's records to an app of their own. Create an app called, for example, "AI gateway" and use its key for LiteLLM or Logpush. The gateway app then shows spend across everything that goes through the gateway, and each app shows its own calls.

  • Don't add the totals together. When your apps also report their calls, adding the gateway app's spend to your other apps' spend counts each call twice. Read spend from one or the other. Organisation budgets and spend pools already count each member app one way only.

  • Map consumers to apps. If you'd rather see a consumer's calls under one of your apps, map its label to that app in the gateway app's settings. The calls then show only there. A label can map only to an app in the same organisation. A label with no mapping stays on the gateway app.

  • Pick a side when both land in one app. If one app records its own AI calls and also receives the gateway's records for them (a gateway sending with the app's own key, or a consumer mapped to it), set AI gateway double counting under Settings › App › Data & privacy:

    • Count this app's own AI calls: the gateway's records are kept but not counted.
    • Count the AI gateway's records: the app's own calls to that gateway's address are kept but not counted, so the gateway's reported cost is used.
    • No rule (the default) sets nothing aside. It suits the set-up above, where the gateway has an app of its own.

    The setting applies to calls arriving from about a minute after you save. In the API it is gateway_spend_source ("app", "gateway" or null) on PUT /api/apps/{id}/privacy (see API).

Budgets and pooled credits

Use this set-up when several apps, possibly in several Heystack organisations, share provider credits behind one gateway. It gives you spend per app, budgets with alerts, a shared pool total across organisations and, if you want it, a soft stop when a budget runs out.

  1. Create one "AI gateway" app in the organisation that runs the gateway. Point LiteLLM's OpenTelemetry callback at Heystack with that app's server key (see LiteLLM). This app receives only the gateway's records, which keeps every call counted once.
  2. Give each consuming app its own label. Create one LiteLLM team per consuming app (or one key alias per app, if you do not use teams). Heystack uses the team alias, else the key alias, as the call's consumer label. Labels must be unique within the gateway.
  3. See spend. AI & Safety › Overview shows spend across all providers, by provider, model, app and consumer, for the month so far with a forecast for the month.
  4. Set a budget per consuming app. In Settings › Organisation › AI budgets, add a budget for the consumer label on the gateway app. If you have mapped a label to one of your apps, an app budget on that app works too. Budgets run per day or per month and alert at 50%, 80% and 100% by default, plus an "on pace to run out" notice once three days of spend show the month will overshoot.
  5. Share credits across organisations (optional). Create a spend pool in Settings › Organisation › AI budgets and invite each app by its organisation and app id, with its gateway label. An admin of each invited organisation must accept before that app counts. Set a pool budget to be alerted on the pool total. You see every member's spend by provider; each member organisation sees only the pool total, the pool budget and its own share.

Cloudflare AI Gateway works the same way, with the heystack_consumer metadata key as the label.

How each app's spend is counted

  • Own telemetry (the default for a pool member): the member app's own AI calls, from its own Heystack set-up or its own gateway.
  • Shared gateway (when you give the member a gateway label): the calls in the pool owner's gateway app that carry that label. The member's own records are then not counted, so nothing is counted twice. A label only ever matches the pool owner's records, never another organisation's.

Cost is the amount the gateway or provider reports for the call, else an estimate from public prices. A call with neither is counted as unpriced and is never treated as $0.

Three rules keep spend and budgets honest (the last two also apply to the cost on AI Health and to a person's AI cost in the security overview):

  • Only model calls count. Agent, tool and retrieval steps around a call (the Vercel AI SDK's invoke_agent record, for example) repeat the call's model and tokens, so they are not counted again.
  • Only cost sent with a server key counts. Calls recorded with a Browser or mobile key (anyone using your app can read those keys) count as calls with their tokens, but as unpriced, so a visitor can never use up a budget. Send AI calls from your server or your gateway.
  • One call counts at most $100. A larger reported or estimated figure is cut to $100.

Heystack's own trace sampling (the app's sample rate, applied when data arrives) never drops AI calls: an app that keeps only a share of its traces still has every model, tool and agent record stored, with its events, so spend and budgets see every call that reaches Heystack. Sampling done in your app before sending (an SDK or collector sampler) happens before Heystack sees the call, so calls it drops are not counted.

Budgets alert; enforcement is a soft limit

A budget alerts you through your notification channels. It does not block anything unless you turn on Enforce for it and switch on the spend limit in the app's safety policy. Then the Heystack guard refuses AI requests it checks for that app once the budget is used up.

This is a soft limit, a budget guard rather than a meter. Spend reaches Heystack about a minute after the call, and only calls whose cost is reported or can be priced count. Whatever is spent in that minute can go over the budget.

For a limit that must never be exceeded, set a hard cap in the gateway itself: LiteLLM virtual-key budgets (max_budget on a key or team) stop requests at the gateway the moment the limit is reached. A common pattern is a LiteLLM hard cap slightly above the Heystack budget, so Heystack warns you first and the gateway is the final stop.

What the gateway never sees from Heystack

Heystack doesn't sit in your gateway's path and never receives your provider keys. It only reads the records your gateway sends. Read-only keys never see prompt or answer text from any gateway.