PARAS MADAN
All writing

Latest in AI

Cloudflare AI Gateway Metadata: Test the 10-Field Limit Before Routing on It

Keep cost and routing tags within Cloudflare AI Gateway's request limits and check what the gateway saves, not just what your app sends.

Your app sends a request tagged as coming from the support team. The model answers, but the log has no team tag. If you also route on that tag or use it to assign spend, a successful response tells you very little about whether the request followed your policy.

Cloudflare raised AI Gateway's custom metadata limit on November 30, from five entries to ten per request. It also separates Cloudflare-added metadata from your quota. The change lets you carry more fields; it doesn't make a tag mandatory for every request.

What actually gets saved

The custom metadata documentation gives you two ceilings: ten custom entries and 12,288 bytes of UTF-8 JSON. If you send eleven fields, the gateway saves the first ten. If the saved fields are too large in total, it removes entries from the end until the rest fit.

Use strings, numbers and booleans for the values. Don't send nested objects or put customer-supplied fields under the reserved cf. prefix. If your app records team and plan separately, send them as two flat fields rather than embedding a customer profile in a metadata value.

Build a short policy vocabulary

For a support assistant, I'd start with cost_center, environment, service and traffic_class. Use stable IDs or short values controlled by the app. Don't pass the body of a support ticket, credentials or free-form instructions in something that ends up in logs.

Assemble and validate the final header after you add client, application and request tags. An individual SDK wrapper can pass its own test while the combined request exceeds the field count. Put required policy fields first in the serialized object as a fallback, but reject or trim optional extras in your own wrapper rather than relying on server truncation. For Node, count UTF-8 bytes using Buffer.byteLength(JSON.stringify(metadata), 'utf8'), not the number of characters.

Check which identity created the tag

The gateway can add cf.user_id on a custom domain protected by Cloudflare Access. The docs describe it as the verified subject of the Access identity, not an email, and it does not replace one of your custom entries. Requests using service tokens or with no user subject do not get that field.

If your app serves both people and scheduled jobs, deliberately test both paths. A route configured only for an authenticated user ID may have nothing to match when a job runs. Keep user identity separate from the cost center, which your backend should assign from trusted configuration.

Test the policy with four requests

Use a non-production gateway and one benign prompt. Start with four cases and record the saved fields, the route taken and the spend condition that matched, if you enabled one:

CaseWhat to check
Normal requestAll required tags appear and the expected conditions match.
More than ten fieldsYour wrapper rejects it or drops optional fields before sending.
Oversized JSONYour validator counts bytes and does not lose a required tag.
Request without a user subjectThe intended service route or explicit fallback still applies.

If you want to observe truncation directly, make a separate test with harmless dummy values and inspect the gateway log. Do not assume a missing field always takes the same route: the result depends on the conditions and fallbacks you configured.

Treat a missing required tag as a failed test

For a spend or routing rule, the value that matters is the one the gateway saved and evaluated. Logging your outgoing object is useful for debugging, but it cannot prove the policy matched. If a tag is mandatory, check it before sending and make the missing-tag behavior explicit.

I'm Paras. I can help you build a small release test for how your AI gateway tags, routes and accounts for requests. Book a 30-minute call.