Setup, explained · from Antigravity to your terminal

Running Cloudflare AI models on pi

You've used Google Antigravity, where the agent, the models, and the billing come pre-wired. Here you assemble the same thing yourself — and learn exactly where it breaks.

6 interactive parts Windows · beginner poke everything

01 The one big idea

Google Antigravity is an agentic IDEA code editor with an AI agent living inside it — you chat, it reads and edits your files and runs commands.: you chat with an agent, pick a model from a dropdown, and it spends quota tied to your plan. The agent, the model access, and the billing are bundled and pre-wired. You open it and type.

pi is that same agent, unbundled — delivered as text in a terminal, free to point at any AI provider. Wire it to Cloudflare and you rebuild the whole Antigravity experience from parts:

The agent chat in the editor pi, run in a terminal
The model dropdown a model name in a config file (@cf/…)
Your Google login + plan a Cloudflare account + an API token
Credits that refill on a timer Neurons, Cloudflare's daily AI allowance
The idea to hold

Everything Antigravity hides behind a graphical shell, you provide as three things: an agent (pi), a credential (one specific kind of token), and a route to the model (a URL in a config file). Get those three right and it just works. Get the token or the config subtly wrong and it fails in ways that look random — which is exactly what the rest of this page teaches you to see coming.

02 How one request actually travels

When pi answers you, it sends an HTTP request to a Cloudflare model. Two things ride along: an Authorization headerA line on the request that carries your secret token, proving who you are — like showing a badge at a door. carrying your token, and a base URLThe address the request is sent to. Change it and the same request goes somewhere else — this is the one lever that routes you through the Gateway. that decides where it lands. Flip the switch and watch the path change:

// the request path

route
π
piAuthorization: Bearer <API_TOKEN>
AI Gatewaygateway.ai.cloudflare.com/v1/<ACCOUNT_ID>/<GATEWAY_ID>/workers-ai/v1
Cloudflare Workers AI · directapi.cloudflare.com/client/v4/accounts/<ACCOUNT_ID>/ai/v1
the model@cf/qwen/qwen3-30b-a3b-fp8 → your answer

Where this lives

That switch is a single line in a file called models.json: set baseUrl to the Gateway address and every model call detours through it — same token, same models, now with a dashboard of logs. Leave it out and you go direct. That's the entire difference.

03 Why it fails intermittently

The number-one beginner trap isn't a typo — it's using the wrong kind of token. Cloudflare has two, and they look similar but behave nothing alike. Pick one and scrub time forward to see what a real setup does over the following days:

// same setup, 3 days of requests

which token did you paste?
0h
Gotcha you can trigger

A token starting with cfoat_ is a Cloudflare OAuth Access Token — the short-lived kind wrangler login mints. It's valid right after sign-in, then its refresh chain silently lapses and every call returns 401/403. Because it works at first, the failure looks random and shows up hours later. Scrub the OAuth timeline past its expiry and watch green turn to red. The dashboard API token has no expiry — it stays green forever.

Good news

You've installed nothing — no wrangler — so you can't accidentally make a cfoat_ token. You create the right one with clicks in the Cloudflare dashboard, scoped to Workers AI + AI GatewayThree permission rows on the token: Workers AI · Read, AI Gateway · Read, AI Gateway · Edit — all set to "Account". The Edit row lets a default gateway create itself.. Ignore any tutorial that says wrangler login.

04 The file that voids itself

pi reads your secret from ~/.pi/agent/auth.json. Three tiny mistakes each break it in a different, silent way — and beginners hit all three. Build the file with the switches and watch pi's verdict update live:

// ~/.pi/agent/auth.json

token type
where does the key go?
json hygiene

      
Three ways to fail silently

1 · Key in the wrong place. pi reads the secret from the top-level "key" field only. Put it under env.CLOUDFLARE_API_KEY and pi acts like it has no credentials — no error, just nothing.
2 · A trailing comma. The file is parsed with strict JSON. One comma after the last item throws, and that voids the whole file: pi ignores your credential and refuses to save new ones.
3 · The wrong token (from part 03). Toggle each switch and read how the verdict changes.

05 When a model returns nothing

One last surprise. Some models come back empty — no error you can see, just blank. It's a token-budget overflow, and it's a knob you can watch tip over. pi asks the model to reserve room for its reply; if the input + reserved replyEvery request spends tokens on your prompt (input) plus the space reserved for the model's answer (completion). Their sum must fit under the model's real context limit. exceeds the model's real limit, Cloudflare rejects the whole call.

// @cf/zai-org/glm-5.2 · real ceiling = 256,000 tokens

262,144
input (~4k) + reserved reply
The mismatch

pi's built-in metadata claims glm-5.2 holds 262,144 tokens, but Cloudflare's real cap is 256,000. Left at the default, pi reserves the whole (wrong, bigger) window for the reply, so input + reply overshoots by a hair and every call returns a 400 — surfacing as empty output. The fix is one line in models.json: cap maxTokens to a sane 32000. Slide it down and watch the gauge drop under the ceiling.

06 Check yourself, then recap

You've seen every failure mode. One question to prove it stuck:

// it worked yesterday, today it throws 401 on every call. most likely?

And a word on cost, so nothing surprises you: Cloudflare meters AI in NeuronsCloudflare's unit for the GPU work a request uses. 10,000 free every day on any plan, resetting at 00:00 UTC.. Every account gets 10,000 free per day (reset at 00:00 UTC); past that you either wait for the reset or upgrade to Workers Paid at $0.011 / 1k. If calls suddenly fail after a heavy day, that's the meter — not your config.

The one sentence

pi + Cloudflare is Antigravity unbundled: an agent you run, a long-lived API token (never cfoat_) placed in the top-level "key" of comma-clean JSON, and a base URL that routes you direct or through the Gateway. Three things right, and the terminal answers back.