Skip to content

Add AI binding support for pi - #123

Open
Maximo-Guk wants to merge 3 commits into
mainfrom
maximo/add-ai-binding-support-for-pi
Open

Add AI binding support for pi#123
Maximo-Guk wants to merge 3 commits into
mainfrom
maximo/add-ai-binding-support-for-pi

Conversation

@Maximo-Guk

@Maximo-Guk Maximo-Guk commented Aug 10, 2026

Copy link
Copy Markdown
Member

Why?
We want to enable users to use AI gateway without needing to provide an API token ( which puts them at risk of credential expiry, rotation, etc ) and makes them need to visit the dashboard just to provision a token during the deploy flow.

How?

Workers AI Gateway can be accessed over the AI binding!

AI.fetch(https://workers-binding.ai/ai-gateway/gateways/platform-gateway/*)

How was this tested?

You can test it with the updated deploy flow which is based off this branches head https://gadgets-deploy.gadgets-staging.workers.dev/deploy ( notice you don't need to provision an api token ), and also locally.

ask-bonk[bot]

This comment was marked as resolved.

@ask-bonk

This comment was marked as resolved.

CF_AI_GATEWAY_API_TOKEN=...

# To send Workers AI straight to its REST endpoint (no gateway, no cost logs):
CF_AI_GATEWAY_WAI_DIRECT=true

@Maximo-Guk Maximo-Guk Aug 10, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's unclear to me why we introduced a CF_AI_GATEWAY_WAI_DIRECT env var in the first place, I don't see why we'd want workers-ai traffic to not go through the gateway. I think the CF_AI_GATEWAY_USE_BINDING=false makes a lot more sense, it opts out all the providers from flowing through the binding transport and with CF_AI_GATEWAY_ACCOUNT_ID allows you to use a cross account gateway for all providers

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this may have been an artifact of an early misunderstanding where I didn't realize AI Gateway was available via the Workers AI binding. Yeah I think we can remove.

@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch from 59cd827 to 595d66d Compare August 10, 2026 15:56
@ask-bonk

This comment was marked as resolved.

@ask-bonk

This comment was marked as resolved.

@kentonv

kentonv commented Aug 11, 2026

Copy link
Copy Markdown
Member

Looks like upstream PR was merged, shall we update to use that?

@Maximo-Guk

Copy link
Copy Markdown
Member Author

Just waiting on a PI release before we can use the upstreamed changes, I'll put up a PR to update once it gets released.

The vendored transport changes in this PR are identical to what got merged in to PI, I would like to get this out so we can start battle testing what we upstreamed in to PI.

@Maximo-Guk
Maximo-Guk marked this pull request as draft August 13, 2026 20:07
@Maximo-Guk

Copy link
Copy Markdown
Member Author

Drafting this for the time being as I work through on updating the PI implementation to be smarter & more efficient

@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch from 595d66d to 24fbea1 Compare August 14, 2026 14:51
@github-actions github-actions Bot added kernel Changes to the Workshop kernel delivery Changes to CI or release delivery labels Aug 14, 2026
@ask-bonk

This comment was marked as resolved.

@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch from 24fbea1 to 0672c92 Compare August 14, 2026 14:57
@ask-bonk

This comment was marked as resolved.

@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch 2 times, most recently from 9b54ad4 to f8704a2 Compare August 15, 2026 02:19
@ask-bonk

This comment was marked as resolved.

@ask-bonk

This comment was marked as resolved.

@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch from f8704a2 to 6f18507 Compare August 15, 2026 20:17
@ask-bonk

This comment was marked as resolved.

@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch from 6f18507 to 976a0e7 Compare August 15, 2026 20:44
@ask-bonk

This comment was marked as resolved.

…ays rides the gateway

The WAI knobs existed because Workers AI originally ran on the account-local
WORKERS_AI binding, which can only name same-account gateways; a cross-account
CF_AI_GATEWAY (the internal production shape) therefore needed an escape
hatch. Workers AI has since moved to ordinary gateway routes
(.../{account}/{gateway}/workers-ai/v1), which work cross-account over
HTTPS+token exactly like anthropic/openai -- so the escape hatch no longer
pays for its config surface.
@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch from 976a0e7 to 00d7293 Compare August 18, 2026 02:30
@ask-bonk

ask-bonk Bot commented Aug 18, 2026

Copy link
Copy Markdown

github run

@ask-bonk

ask-bonk Bot commented Aug 18, 2026

Copy link
Copy Markdown

@Maximo-Guk Bonk workflow failed. Check the logs for details.

View workflow run · To retry, trigger Bonk again.

@github-actions

Copy link
Copy Markdown

Preview: pr123-maximo-add-ai-3d199549

https://pr123-maximo-add-ai-3d199549-router.cloudflare-os-previews.workers.dev

Dashboard · deleted when this PR closes

Binding-routed gateway traffic went through gateway().run(), the
universal endpoint's [{provider, endpoint, headers, query}] envelope,
which a vendored shim built by splicing the request body into a JSON
string. The gateway also serves its provider-native passthrough over the
AI binding at /ai-gateway/gateways/{gateway}/{provider}/... -- the HTTPS
path minus the account id, since the binding channel carries identity --
and that route accepts exactly the requests pi's API impls already
produce.

So there is nothing left to translate. Binding-routed models take the
binding host as their gateway root and pi's fetch option is the binding's
own fetch, unwrapped; the envelope, the splice, the JSON-object scanner
and the whole vendored ai-gateway-binding-fetch module go away. Method,
headers, query string and the body stream ride through untouched, so
multi-MB prompt bodies are never copied in the isolate.

Google keeps its HTTPS root automatically: `binding` is resolved per
provider, and bindingFor() returns undefined for google (its adapter
can't take a custom fetch). cf-aig-authorization now reaches the gateway,
which recognizes the pre-authentication sentinel and strips it.

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
@Maximo-Guk

Copy link
Copy Markdown
Member Author

bonk break glass

@github-actions github-actions Bot added the bonk-break-glass Skip automatic Bonk PR review label Aug 18, 2026
@Maximo-Guk
Maximo-Guk force-pushed the maximo/add-ai-binding-support-for-pi branch from 00d7293 to 945eb5e Compare August 18, 2026 02:42
@Maximo-Guk
Maximo-Guk marked this pull request as ready for review August 18, 2026 02:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bonk-break-glass Skip automatic Bonk PR review delivery Changes to CI or release delivery kernel Changes to the Workshop kernel

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants