All skills
cloudflare avatar

/agents-sdk

@41e0d19 official
by cloudflarecloudflare/skills3k stars
298

Build, debug, or review Cloudflare Agents SDK applications using the agents package.

Use this Skill: https://skilld.dev/gh/cloudflare/skills/agents-sdk

This session only. Nothing lands on disk.

referencesthink.md

≈734 tokens on demand. Your agent reads this file only when SKILL.md points to it.

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

Source: SKILL.md on GitHub

No alerts16d5 checks · Risk SAFE
  • Gen Agent Trust Hub16d

    This skill provides reference documentation and configuration guidelines for building, debugging, and reviewing applications using the Cloudflare Agents SDK. It consists entirely of educational markdown files and contains no executable code or security risks.

  • Socket16d

    No alerts

  • Snyk16d

    Risk: LOW · No issues

  • Runlayer7mo

    8 files scanned · No issues

  • ZeroLeaks5mo

    Score: 93/100 · 2 sections analyzed

Signed by skilld at 41e0d19. This ties the file your Agent reads to that commit on GitHub. It does not review the instructions.

Last checked against GitHub 5 hours ago.

Activeupdated 7 hours ago
  • MCP
  • cloudflare
  • workers
  • agents
  • state-management
  • websocket
  • durable-objects
  • scheduling
  • workflows
  • observable

README badge

README badge for cloudflare/skills/agents-sdk

Builds stateful AI agents on Cloudflare Workers using Durable Objects, with APIs for persistent state, callable RPC methods, scheduling, workflows, durable execution, queues, and streaming chat. Covers the full lifecycle of agent development from configuration through observability, with experimental support for voice, browser automation, and MCP integration.

Generated from the current SKILL.md.

Does this work with existing Cloudflare Workers projects?
Yes. Use the 'Add to existing project' guide to install the SDK into an existing Workers app. You'll need to configure durable objects and migrations in wrangler.jsonc.
What state management does the SDK provide?
SQLite-backed persistent state that auto-syncs to clients via setState(). State changes trigger validateStateChange() and onStateUpdate() hooks, and the SDK includes built-in SQL query support.
Can I use this with chat applications?
Yes. The SDK includes AIChatAgent for streaming chat with tools, message persistence, and resumable streams. It requires @cloudflare/ai-chat and ai packages.
Does this support background workflows and scheduled tasks?
Yes. The SDK provides AgentWorkflow for durable multi-step tasks, schedule() / scheduleEvery() for one-time and recurring tasks, and runFiber() for work that survives durable object eviction.
Can I connect to external MCP servers or build MCP servers with this?
Yes. The SDK includes MCP client integration to connect to external MCP servers and McpAgent to build MCP servers with configurable transports (HTTP, SSE, RPC).

Generated from the current SKILL.md. These answers refresh after source changes.