Basic Usage
Create a script file in yourscripts/ directory:
Configuration
AgentExecutionContext:
context.input- The input data passed to the agentcontext.env- Environment bindings (KV, D1, etc.)context.logger- Structured loggingcontext.request- HTTP request (if triggered via HTTP)context.cache- Caching utilitiescontext.location- Geographic and compliance datacontext.edge- Network and protocol infocontext.telemetry- Emit business metrics
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 viacontext.env:
Error Handling
Throw Errors
Return Error State
Directory Organization
Organize your scripts by domain:Best Practices
1. Keep Scripts FocusedMigration from Inline Code
If you have existing ensembles with inline code, migrate them to script files:Before (Not Supported in Workers)
After (Recommended)
Key Differences
Testing
Test your scripts with full TypeScript support:When to Use Custom Agents Instead
Use full custom agents (withagents/ directory) when you need:
- Multiple related operations - Group related functionality
- Complex state management - Shared state across operations
- External dependencies - npm packages
- Database access - Complex queries
- Reusable across projects - Publish as agent package
Next Steps
think Operation
AI reasoning
storage Operation
Data persistence
Script Components
Versioned scripts
Playbooks
Common patterns

