Cloudflare Workers

medium10 minsrc/index.ts

You want the agent API on Cloudflare’s edge, with the API key kept as a Worker secret.

What it shows

  • Read secrets from env in cloudflare:workers
  • Export cogitatorApp() as the Worker

The code

src/index.ts
import { env } from 'cloudflare:workers';
import { Agent, Cogitator, tool } from '@cogitator-ai/core';
import { cogitatorApp } from '@cogitator-ai/hono';
import { z } from 'zod';

interface Env {
  GOOGLE_API_KEY: string;
}

const calculator = tool({
  name: 'calculator',
  description: 'Add, subtract, multiply or divide two numbers',
  parameters: z.object({ a: z.number(), b: z.number(), op: z.enum(['+', '-', '*', '/']) }),
  execute: async ({ a, b, op }) => ({
    result: op === '+' ? a + b : op === '-' ? a - b : op === '*' ? a * b : a / b,
  }),
});

const assistant = new Agent({
  name: 'assistant',
  model: 'google/gemini-3.5-flash-lite',
  instructions: 'You are a helpful assistant. Use the calculator for arithmetic. Be concise.',
  tools: [calculator],
  temperature: 0.3,
});

const cogitator = new Cogitator({
  llm: { providers: { google: { apiKey: (env as Env).GOOGLE_API_KEY } } },
});

export default cogitatorApp({ cogitator, agents: { assistant } });

Run it

install · bash
npm install @cogitator-ai/core @cogitator-ai/hono hono zod
npm install -D wrangler @cloudflare/workers-types
EnvironmentGOOGLE_API_KEY
run · bash
echo "GOOGLE_API_KEY=your-key" > .dev.vars
npx wrangler dev

Or run the full example from a clone of the repository (keys in .env):

repository · bash
cd examples/integrations/10-cloudflare-worker && npm install && npm run dev

wrangler.jsonc

wrangler.jsonc · jsonc
{
  "name": "cogitator-agent",
  "main": "src/index.ts",
  "compatibility_date": "2026-09-01"
}

Deploy

Deploy · bash
npx wrangler secret put GOOGLE_API_KEY
npx wrangler deploy

Learn more