Skip to main content

What’s an Agent?

An agent is a reusable unit of work with:
  • Inputs: Parameters it accepts
  • Operation: What it does (code, think, http, storage, etc.)
  • Outputs: Data it returns
Agents are automatically discovered from the agents/ directory at build time (v1.12+) and can be used across multiple ensembles.

Explore the Template Agent

Your project already includes a working agent! Let’s explore agents/examples/hello/:

agent.yaml

This declares:
  • Operation type: code (runs JavaScript/TypeScript)
  • Input schema: Accepts name (required) and style (optional)
  • Output schema: Returns a message string

index.ts

Note the signature: Agents use AgentExecutionContext which provides:
  • input - Your agent’s parameters
  • env - Cloudflare bindings (KV, D1, AI, etc.)
  • ctx - ExecutionContext (waitUntil, etc.)
This signature works everywhere: direct calls, ensembles, and tests.

Understanding Operation Types

Agents use different operations based on what they need to do:

operation: code

When to use: Run custom TypeScript/JavaScript logic Requires: Function implementation in index.ts API keys needed: ❌ No

operation: think

When to use: Call LLM models for reasoning, analysis, generation Requires: Provider and model configuration API keys needed: ✅ Yes (OpenAI, Anthropic) or Cloudflare Workers AI

operation: http

When to use: Make HTTP requests to external APIs Requires: URL and method configuration API keys needed: Depends on API

operation: data

When to use: Query databases (KV, D1, R2) Requires: Database binding in wrangler.toml API keys needed: ❌ No (uses Cloudflare bindings)
See all operations

Critical: Agent Signatures for Ensembles

All agents MUST use the AgentExecutionContext signature to work in ensembles!

The Correct Pattern ✅

Why This Signature?

When called through an ensemble, Conductor wraps your parameters:
Benefits:
  • ✅ Works in ensembles (orchestrated workflows)
  • ✅ Works with direct calls
  • ✅ Works in tests
  • ✅ Access to Cloudflare bindings (env)
  • ✅ Access to ExecutionContext (ctx)

Wrong Pattern (Don’t Do This) ❌

This only works for direct function calls, but fails in ensembles because the parameters are wrapped in input.

Using env and ctx

The signature gives you access to powerful features:
Quick Rule: Always use AgentExecutionContext signature. It’s the only pattern that works everywhere!

Test the Hello Agent

The template includes working tests. Let’s look at tests/basic.test.ts:
Run it:
All tests should pass! ✅

Create Your First Custom Agent

Now that you understand how agents work, let’s create a new one.

Step 1: Create Agent Directory

Step 2: Define the Agent

Create agents/user/greeter/agent.yaml:

Step 3: Implement the Agent

Create agents/user/greeter/index.ts:

Step 4: Rebuild

Agents are auto-discovered at build time:

Step 5: Use Your Agent

Create ensembles/greeting-workflow.yaml:

Step 6: Test It

Create tests/greeter.test.ts:
Run: pnpm test

Agent with AI (operation: think)

Let’s create an agent that uses AI for more complex logic.

Create AI Analyzer Agent

Create agents/user/analyzer/agent.yaml:
Note: This requires an Anthropic API key in your environment.

Use the Analyzer

Create ensembles/analyze-text.yaml:

Auto-Discovery (v1.12+)

Zero-Config Agent Loading: Agents in the agents/ directory are automatically discovered at build time and registered with your application.

How It Works

  1. Build-Time Discovery: Vite plugins scan agents/**/*.yaml during build
  2. Virtual Modules: Agent configs and handlers are bundled into virtual:conductor-agents
  3. Runtime Registration: MemberLoader.autoDiscover() loads all discovered agents automatically

Creating a New Agent

Just create the files - no imports or registration needed:
  1. Create the directory: agents/user/my-agent/
  2. Add agent.yaml (required)
  3. Add index.ts (optional, for operation: code)
  4. Rebuild: pnpm run build
  5. Done! Your agent is now available at /api/v1/execute/agent/{name}

Using Auto-Discovered Agents

With the auto-discovery API (recommended):
That’s it! No manual imports. No registration code. Just create YAML files.

Verification

List all discovered agents:

Testing with Manual Registration

Note: In unit tests, you can still use manual registration for clarity:
This is fine for tests! Manual registration is supported alongside auto-discovery.

Migration from v1.11

If you have existing manual registration code in your entry point: Before (v1.11):
After (v1.12):
Saves 400+ lines of boilerplate! See the Auto-Discovery guide for complete details.

Agent Patterns

Pattern 1: Simple Code Agent

Pure logic, no external dependencies:

Pattern 2: HTTP Data Fetcher

Fetch from external APIs:

Pattern 3: Database Query

Query Cloudflare D1:

Pattern 4: AI with Custom Logic

Combine AI with code:

Best Practices

1. Keep Agents Focused

Each agent should do ONE thing well:
  • user-validator - Validates user data
  • user-handler - Validates, stores, sends email, logs (too much!)

2. Use Descriptive Names

  • email-sender, pdf-extractor, sentiment-analyzer
  • helper, utils, processor

3. Document Inputs/Outputs

Always define schemas:

4. Handle Errors Gracefully

5. Use Caching for HTTP Agents

Reduces API calls and improves performance!

6. Test Your Agents

Always write tests for custom agents:

Troubleshooting

Problem: Created a new agent but it’s not availableFix: Rebuild to trigger auto-discovery:
Agents are discovered at build time, not runtime.
Problem: TypeError: this.ctx.waitUntil is not a functionFix: Use proper ExecutionContext mock:
Problem: operation: think fails with authentication errorFix: Add API key to wrangler.toml or environment:
Or use Cloudflare Workers AI (no key needed):
Problem: Agent works when called directly but fails in ensemblesCause: Agent not using AgentExecutionContext signatureFix: Update agent signature:
See Agent Signatures section above.
Problem: Want to use an operation that doesn’t existFix: Use operation: code and implement in TypeScript:
Code operations can do anything TypeScript can do!

TypeScript Agent Handlers

Every agent with operation: code needs a TypeScript handler. Here’s everything you need to know about writing effective handlers.

Handler Structure

The AgentExecutionContext

All handlers receive the same context object:

Handler Patterns

Simple Synchronous Handler:
Async Handler with External APIs:
Handler with Cloudflare Bindings:

Using Agents in TypeScript Ensembles

Once you have YAML agents with TypeScript handlers, you can reference them in TypeScript ensembles:

Validating Agents

Validate your agent configurations:

Next Steps

Your First Ensemble

Combine agents into workflows

TypeScript API

Complete TypeScript reference

Starter Kit

Use ready-made agents

Testing Guide

Write comprehensive tests

Advanced: Versioning with Edgit (Optional)

If you want component-level versioning, you can use Edgit:
Note: Edgit is optional. Standard git version control works great!
Learn more about Edgit