think.md

Created about 12 hours ago

attributes
source_pathreferences/think.md
source_sha2569ad3c294f7feef292cdbe6ece0ac916054e4604af00e91fd02a4216fc4ad9df5
source_urlhttps://github.com/cloudflare/skills/blob/47dfc2ce969bf62acf8bc231198ade4bb02712e0/skills/agents-sdk/references/think.md
supporting_skill_id40176
upstream_revision47dfc2ce969bf62acf8bc231198ade4bb02712e0

Think (Experimental)

Fetch https://developers.cloudflare.com/agents/harnesses/think/index.md for complete documentation.

@cloudflare/think — a higher-level chat agent class that handles the streamText loop, tool execution, and message persistence for you. You provide getModel() and getSystemPrompt(); Think handles the rest.

npm install @cloudflare/think

Minimal Agent

import { Think } from "@cloudflare/think";
import { createWorkersAI } from "workers-ai-provider";
import { routeAgentRequest } from "agents";

export class MyAgent extends Think<Env> {
  getModel() {
    return createWorkersAI({ binding: this.env.AI })("@cf/meta/llama-4-scout-17b-16e-instruct");
  }

  getSystemPrompt() {
    return "You are a helpful assistant.";
  }
}

export default {
  fetch: (req, env) => routeAgentRequest(req, env)
};

Wrangler Config

{
  "compatibility_flags": ["nodejs_compat", "experimental"],
  "durable_objects": {
    "bindings": [{ "name": "MyAgent", "class_name": "MyAgent" }]
  },
  "migrations": [{ "tag": "v1", "new_sqlite_classes": ["MyAgent"] }],
  "ai": { "binding": "AI" }
}

Note: Think requires the experimental compatibility flag.

Custom Tools

import { tool } from "ai";
import { z } from "zod";

export class MyAgent extends Think<Env> {
  getTools() {
    return {
      getWeather: tool({
        description: "Get weather",
        parameters: z.object({ city: z.string() }),
        execute: async ({ city }) => `72°F in ${city}`
      })
    };
  }
}

Lifecycle Hooks

Hook When Use for
configureSession() Agent starts Set up memory, context providers
beforeTurn(ctx) Before each LLM call Per-turn model/tools/system prompt; return TurnConfig
onChunk(chunk) Each streaming chunk Progress tracking
onChatResponse(result) After LLM turn completes Chaining, follow-up saveMessages
onChatError(error) On LLM error Error handling
async beforeTurn(ctx: TurnContext): Promise<TurnConfig> {
  if (ctx.continuation) {
    return { model: cheaperModel };
  }
  return {};
}

Sub-Agents

const child = this.subAgent(SpecialistAgent, "specialist-1");
await child.chat("Analyze this data...", (chunk) => {
  // stream callback
});

Client

Same React hooks as AIChatAgent:

const agent = useAgent({ agent: "MyAgent", name: "session-1" });
const { messages, input, handleInputChange, handleSubmit } = useAgentChat({ agent });

Think vs AIChatAgent

Think AIChatAgent
streamText loop Built-in You write it
Tool execution Automatic You wire it
Customization Override hooks Full control in onChatMessage
Built-in tools Workspace, execute, browser None
Compatibility flag Requires experimental Standard

Children

No children yet.

Like how this is organised?

Build your own Memory tree on knowen.md: notes, decisions and tasks in one connected markdown graph, shared with your team and your AI agents.