Skip to main content
Private beta: The LLM Gateway is in private beta. Sign up for the waitlist to get access.
A spend policy or rate limit policy can carry a condition on a custom request header, so traffic from a single subject splits into separate limits by header value. Use this to cap each of your own end customers, tenants, or teams without issuing a separate LangSmith API key for each one. For example, a policy scoped to a workspace with the condition X-Gateway-Customer-Id: acme limits only the requests from that workspace that carry that header value. Requests from the same workspace carrying X-Gateway-Customer-Id: globex count against a different policy.

Matchable headers

The gateway matches on request headers prefixed with X-Gateway-, and on keys inside the X-Gateway-Metadata JSON header. No other request header is matchable. Header names are normalized before matching: the X-Gateway- prefix is stripped, the remainder is lowercased, and every character outside a-z, 0-9, and _ is replaced with _. The headers X-Gateway-Customer-Id, x-gateway-customer_id, and X-Gateway-CUSTOMER.ID all resolve to the matcher key customer_id. Header values are compared as exact, case-sensitive strings, with no wildcard or pattern matching. The gateway stamps caller identity itself and ignores client attempts to override it. Headers that resolve to organization_id, workspace_id, workspace_handle, user_id, user_email, api_key_id, api_key_short, auth_mode, user_agent, applied_policy_ids, or applied_policy_names, and any header whose normalized name starts with gateway, are discarded.

Header condition rules

  • One condition per policy: A policy accepts a single header key with a single value.
  • Pairs with one subject scope: Combine a header condition with an organization, workspace, user, or API key scope. The subject side accepts several values and matches any of them. The header side accepts exactly one value.
  • Spend caps and rate limits only: Default policies cannot carry a header condition, so the gateway never creates per-header policies on its own. To limit many header values, create one policy for each.
  • A missing header matches nothing: A request that does not carry the header does not match the policy. Pair per-header policies with a broader policy on the subject itself so untagged traffic is still limited.
  • Every matching policy is enforced: A request that matches both a plain subject policy and a policy with a header condition counts against both, and either one can block it.
  • At most 10 conditions: A policy carries no more than 10 subject conditions in total.

Add a header condition

Creating and managing policies requires the organization:manage permission. For the full permissions breakdown, refer to Traces, Engine, and access control.
  1. Go to Settings → Gateway → LLM Gateway.
  2. Click Create policy.
  3. Select the policy type and subject scope, then set the limits.
  4. Under Custom header condition (optional), enter the Header name without its X-Gateway- prefix (for example, Customer-Id) and the Header value to match (for example, acme).
  5. Save.
You cannot edit the header condition in the UI after the policy is created. To change it, delete the policy and create a new one, or update subject_matchers through the API.

Cap spend per end customer

A reseller or multi-tenant application usually calls the gateway from its own backend, using one workspace-scoped API key on behalf of many end customers. Header conditions give each of those end customers a separate cap under that single key.
The gateway trusts the X-Gateway-* headers on an incoming request. Set the header in your own backend after you authenticate the end user, and do not distribute the gateway API key to end users. A caller that controls both the key and the header can choose which cap to spend against.

Step 1. Send a customer header on every call

Attach the header to each request your backend makes on behalf of an end customer:
If your LangSmith account is on a regional instance, use the corresponding regional gateway.

Step 2. Create a cap for one customer

Create one spend policy per end customer through the LangSmith REST API:
The matcher key is the normalized name customer_id, not the header name X-Gateway-Customer-Id. The policy belongs to the organization that owns the API key. Posting a policy whose subject_matchers already exist updates that policy instead of adding a duplicate, so this call is safe to repeat.

Step 3. Sync policies with your customer list

Because each end customer needs its own policy, keep the policy set in step with your customer list. The following script creates or updates a cap for every current customer, then deletes the caps of customers that are gone:
Run the script whenever a customer signs up, churns, or moves to a different plan.

Step 4. Read spend per customer

Each spend policy returned by the API reports current_spend_usd, the spend accumulated in the policy’s active window. Use it to show each end customer their usage, or to warn them before they reach the cap. The field is omitted when the spend lookup fails, so treat a missing value as unknown rather than as zero. The list endpoint narrows by a subject matcher key only when that key is paired with a value, so list the spend caps and select the per-customer ones in your own code:

Limit throughput per end customer

Rate limits use the same subject matchers. Swap policy_type and config to give an end customer its own request and token allowance:
The sync script in step 3 applies to rate limits with the same two substitutions. Spend caps and rate limits are separate families, so an end customer can hold one of each on the same header value.

Next steps