Skip to main content
Cloudflare Workers Compatibility: Inline code (config.code) is not supported in Cloudflare Workers due to security restrictions. All code must be in script files that are bundled at build time.
Define reusable code scripts as components for consistent business logic across ensembles.

Overview

Script components enable you to:
  • Reuse code logic across multiple agents and ensembles
  • Version scripts with semantic versioning for reproducibility
  • Organize TypeScript code separately from YAML
  • Type-safe development with full TypeScript support
  • Test scripts independently with unit tests
  • Bundle scripts at build time for Workers compatibility

Quick Start

1. Create a Script File

Create a TypeScript file in your scripts/ directory:

2. Reference in Your Ensemble

3. Build & Deploy

Scripts are automatically discovered and bundled during the build:

URI Formats

Both formats are supported for referencing scripts:

Script File Format

Scripts must export a default function that receives AgentExecutionContext:

Async Scripts

Scripts can be async for operations that interact with storage:

TypeScript Types

Full TypeScript support with proper typing:

Directory Organization

Organize scripts by domain:

Multi-Step Workflows

Use multiple script agents for complex data pipelines:
With corresponding script files:

Best Practices

1. Keep Scripts Focused

Each script should do one thing well:

2. Type Your Inputs

TypeScript helps catch errors at build time:

3. Handle Edge Cases

4. Add Error Handling

5. Don’t Mutate Input

Testing Scripts

Test scripts with unit tests:

Integration with Other Operations

With Think Agents

With Data Agents

Versioning with Edgit

For production deployments, use edgit to version and manage scripts:
Reference versioned scripts:

Using ctx API in Agents

When building custom agents with TypeScript handlers, you can access and execute other scripts through the ctx API:

ctx.scripts.get(name)

Get and execute a script by name:

Composing Multiple Scripts

Dynamic Script Loading

Script with Shared Utilities

Error Handling with Scripts

Next Steps

Code Operation

Full code operation docs

Prompt Components

Reusable AI instructions

Query Components

SQL queries as components

Creating Agents

Build custom agents