Skip to content

Forward gateway options as cf-aig-* headers from Ai.run()#6798

Open
JoaquinGimenez1 wants to merge 1 commit into
cloudflare:mainfrom
JoaquinGimenez1:main
Open

Forward gateway options as cf-aig-* headers from Ai.run()#6798
JoaquinGimenez1 wants to merge 1 commit into
cloudflare:mainfrom
JoaquinGimenez1:main

Conversation

@JoaquinGimenez1
Copy link
Copy Markdown
Member

env.AI.run(model, inputs, { gateway }) previously sent the gateway config only inside the JSON request body (options.gateway). AI Gateway enforces these options via cf-aig-* request headers (as the REST API and the Universal endpoint env.AI.gateway(id).run() already do), so fields like requestTimeoutMs were silently ignored on the binding's run path. Long-running calls never aborted, despite the timeout being exposed in the TypeScript types.

Add a gatewayOptionsToHeaders helper that mirrors AiGateway.#getHeadersFromOptions in aig-api.ts, and emit those headers in #generateFetch. The mapping is placed after extraHeaders so the typed gateway option takes precedence. gateway.id is intentionally not emitted as a header; it continues to drive /ai-gateway/run routing via the body. The change is purely additive: the body still carries options.gateway, so existing behavior is unaffected.

Adds a regression test (full option set and requestTimeoutMs-only) that asserts the cf-aig-* headers reach the upstream fetch.

`env.AI.run(model, inputs, { gateway })` previously sent the gateway
config only inside the JSON request body (`options.gateway`). AI Gateway
enforces these options via `cf-aig-*` request headers (as the REST API
and the Universal endpoint `env.AI.gateway(id).run()` already do), so
fields like `requestTimeoutMs` were silently ignored on the binding's
run path. Long-running calls never aborted, despite the timeout being
exposed in the TypeScript types.

Add a `gatewayOptionsToHeaders` helper that mirrors
`AiGateway.#getHeadersFromOptions` in aig-api.ts, and emit those headers
in `#generateFetch`. The mapping is placed after `extraHeaders` so the
typed `gateway` option takes precedence. `gateway.id` is intentionally
not emitted as a header; it continues to drive `/ai-gateway/run` routing
via the body. The change is purely additive: the body still carries
`options.gateway`, so existing behavior is unaffected.

Adds a regression test (full option set and requestTimeoutMs-only) that
asserts the cf-aig-* headers reach the upstream fetch.
@JoaquinGimenez1 JoaquinGimenez1 requested review from a team as code owners June 4, 2026 02:44
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant