Skip to main content
Cloudflare Workers Compatibility: Inline code (config.code) is not supported in Cloudflare Workers due to security restrictions. Workers blocks new Function() and eval() by design. Use config.script to reference bundled script files instead.
Use it for transformations, validations, calculations, and custom logic that doesn’t require AI or external services.

Basic Usage

Create a script file in your scripts/ directory:
Reference it in your ensemble:

Configuration

The script receives the full AgentExecutionContext:

Script File Format

Scripts must export a default function:

Async Scripts

Scripts can be async for operations that need to wait:

URI Formats

Both formats are supported for referencing scripts:

Common Use Cases

1. Data Transformation

2. Calculations

3. Validation

4. Array Operations

5. Conditional Logic

6. Date/Time Operations

7. Location-Aware Logic

Use the location context for geo-aware business logic:

8. Security & Compliance Checks

Use the edge context for security decisions:

Using Previous Agent Outputs

Reference outputs from previous agents via the input mapping:

Accessing Environment

Scripts have access to Cloudflare bindings via context.env:

Error Handling

Throw Errors

Return Error State

Directory Organization

Organize your scripts by domain:
Reference them with their path:

Best Practices

1. Keep Scripts Focused
2. Type Your Inputs
3. Handle Nulls
4. Return Objects
5. Don’t Mutate Input

Migration from Inline Code

If you have existing ensembles with inline code, migrate them to script files:

Before (Not Supported in Workers)

Key Differences

Testing

Test your scripts with full TypeScript support:

When to Use Custom Agents Instead

Use full custom agents (with agents/ directory) when you need:
  1. Multiple related operations - Group related functionality
  2. Complex state management - Shared state across operations
  3. External dependencies - npm packages
  4. Database access - Complex queries
  5. Reusable across projects - Publish as agent package
See Creating Agents for more.

Next Steps

think Operation

AI reasoning

storage Operation

Data persistence

Script Components

Versioned scripts

Playbooks

Common patterns