Getting Started
Build your first AI agent using the TypeScript Agent Framework.
Learn how to create a production-ready conversational AI agent with session management and streaming responses by following the typescript-agent-template. This guide walks through the complete implementation from the typescript-agent-template, showing you how to create a production-ready conversational AI agent with session management and streaming responses.
What You'll Build
By the end of this guide, you'll have a fully functional AI agent that:
- 🧠 Converses intelligently using Anthropic's Claude
- 💬 Maintains conversation context across multiple interactions
- 🔄 Streams responses in real-time for better UX
- 🚀 Scales automatically on Cloudflare's edge network
- 🛠️ Supports MCP tools for extended capabilities
Prerequisites
Before starting, ensure you have:
- Node.js 22+ and pnpm installed
- Cloudflare account with Workers enabled
- Anthropic API key for Claude integration
Quick Setup
1. Clone and Install
2. Configure local Environment
3. Start Development
4. Test Your Agent
Architecture Overview
The agent uses Cloudflare Durable Objects for session management, ensuring:
- Persistent Memory: Conversations maintain context across interactions
- Global Distribution: Sessions accessible from any Cloudflare edge location
- Auto Scaling: Each session runs independently
Code Breakdown
Let's dive into the complete implementation from src/index.ts:
Core Imports and Setup
Key Components:
- Hono: Fast, lightweight web framework for Cloudflare Workers
- CORS: Cross-origin resource sharing for web applications
- SimplePromptAgent: Base agent class from our SDK
- Anthropic: AI provider integration
- Types: TypeScript definitions for message handling
Main Application Setup
This creates the main application with CORS enabled for the /agent/* routes, allowing web applications to communicate with your agent.
The SimplePromptAgent Class
Key Features:
- Extends SimplePromptAgent: Inherits session management and streaming capabilities
- Durable Object State: Persistent storage for conversation history
- Environment Access: Cloudflare Workers environment variables
- Model Integration: AI provider (Anthropic Claude) connection
Message Processing Logic
Process Breakdown:
- Session ID: Unique identifier for conversation continuity
- Messages: Array of conversation messages (user + assistant)
- System Prompt: Defines the agent's personality and behavior
- Max Steps: Limits reasoning steps for cost control
- Streaming: Returns real-time response stream
Chat Endpoint Handler
Endpoint Logic:
- Session Management: Creates new session ID if none provided
- Request Parsing: Extracts JSON message payload
- Durable Object Lookup: Gets or creates session-specific agent instance
- Message Processing: Forwards request to agent for handling
Durable Object Export
This exports the agent class and the main fetch handler for Cloudflare Workers.
Usage Examples
Basic Chat Interaction
Start a new conversation:
Continue existing conversation:
JavaScript Client Implementation
React Hook Integration
Customization Options
1. Modify Agent Personality
2. Add MCP Tools
3. Environment Configuration
Update your wrangler.jsonc:
Deployment
Deploy to Production
Environment Variables
Set these as Cloudflare Workers secrets:
Next Steps
Now that you have a working agent, explore these advanced features:
- Sessions: Deep dive into session handling and state management
- AI SDK Integration: Advanced AI provider configurations and tool calling
- MCP Tools: Add external capabilities to your agents
- Middleware: Custom request/response processing
Troubleshooting
Common Issues
Agent not responding:
- Check your Anthropic API key is set correctly
- Verify the
wrangler.jsoncconfiguration - Review Cloudflare Workers logs with
npx wrangler tail
CORS errors:
- Ensure CORS is enabled for your routes
- Check your client's origin is allowed
Session state lost:
- Verify Durable Objects are properly configured
- Check session ID consistency in your client
Getting Help
- Documentation: Comprehensive guides and API references
- GitHub Issues: Report bugs and request features
- Discord Community: Real-time support and discussions
You've successfully built your first AI agent! The Simple Prompt Agent demonstrates the core concepts of the TypeScript Agent Framework while providing a solid foundation for more complex implementations.