Skip to content

The session agent

The agent runs in the same Durable Object as the terminal. They share one disk, shell, process table, and set of ports. When the agent edits a file, the editor sees it. When it starts a server, the preview tab opens.

It uses the AI SDK with Workers AI’s OpenAI-compatible endpoint. An AI Gateway is optional. Replies stream into the chat pane, with thinking state and tool calls shown inline.

Choose who pays for inference:

  • With user OAuth, each user connects a Cloudflare account through Authorization Code + PKCE and spends their own Workers AI quota. The hosted demo works this way.
  • With an owner token, one API token pays for everyone.

User OAuth tokens never touch Durable Object storage. They live only in encrypted HttpOnly, Secure, SameSite=Lax cookies scoped to the session URL.

In Cloudflare’s Create OAuth client form, use grant type Authorization Code, token auth method None, and redirect URL https://<your-host>/api/nimbus/oauth/callback. Select only the scopes in the table. Don’t select Account API Gateway; that belongs to API Shield, not AI Gateway.

Scope ID Why
user-details.read Read the connected user’s profile
account-settings.read List the user’s accounts
ai.write Run Workers AI inference
aig.run Only when NIMBUS_AGENT_GATEWAY_ID is set
wrangler.jsonc
{
"vars": {
"NIMBUS_CF_OAUTH_CLIENT_ID": "<oauth-client-id>",
"NIMBUS_CF_OAUTH_SCOPES": "<scope-id-1> <scope-id-2>",
"NIMBUS_AGENT_MODEL": "@cf/moonshotai/kimi-k2.6",
"NIMBUS_AGENT_GATEWAY_ID": "default"
}
}
Terminal window
npx wrangler secret put NIMBUS_AGENT_COOKIE_SECRET

For the owner-token path, set NIMBUS_CLOUDFLARE_ACCOUNT_ID in vars and NIMBUS_CLOUDFLARE_API_TOKEN as a secret. For a custom login flow, use the OAuth helpers from @nimbus-sh/sdk/worker (configure the handler).