This directory contains production-ready examples for integrating AI capabilities into your Cloudflare Workers + React application.
Location: simple-claude-chat/
Basic integration with Anthropic's Claude API using non-streaming responses.
- Simple request/response pattern
- Complete message handling
- Error handling and validation
- TypeScript types included
Best for: Chat interfaces where response time is acceptable, simple Q&A systems
Location: streaming-chat/
Advanced Claude API integration with real-time streaming using Server-Sent Events.
- Character-by-character streaming
- SSE (Server-Sent Events) implementation
- Better UX for long responses
- Proper cleanup and error handling
Best for: Conversational interfaces, long-form content generation, improved user experience
Location: workers-ai-chat/
Native Cloudflare Workers AI integration using built-in models.
- No external API needed
- Uses Cloudflare's AI binding
- Multiple model options (Llama, etc.)
- Cost-effective for high volume
Best for: Cost-sensitive applications, edge-native AI, privacy-focused deployments
Location: with-ai-gateway/
Integration with Cloudflare's AI Gateway product for enhanced AI operations.
- Multi-provider routing (OpenAI, Anthropic, etc.)
- Built-in caching
- Analytics and monitoring
- Cost optimization
Best for: Production applications, multi-model strategies, cost tracking
| Feature | Simple Claude | Streaming Chat | Workers AI | AI Gateway |
|---|---|---|---|---|
| Streaming | No | Yes | Partial | Yes |
| Setup Complexity | Low | Medium | Low | Medium |
| API Key Required | Yes | Yes | No | Yes |
| Response Speed | Medium | Fast (perceived) | Very Fast | Medium |
| Cost | Pay per token | Pay per token | Included | Pay per token + caching |
| Model Quality | Best (Claude) | Best (Claude) | Good (Llama) | Varies by provider |
| Privacy | External API | External API | Edge-native | External API |
| Caching | Manual | Manual | No | Built-in |
| Analytics | Manual | Manual | No | Built-in |
- Building your first AI integration
- Response streaming is not required
- You want the simplest implementation
- Learning the basics
- Building production chat interfaces
- Long responses need better UX
- Users expect real-time feedback
- Response times matter
- Minimizing external dependencies
- Cost is a major concern
- You need edge-native processing
- Privacy/data locality is important
- High request volume expected
- Building production applications
- Using multiple AI providers
- Need analytics and monitoring
- Want automatic caching
- Cost optimization is important
- Claude 3.5 Sonnet: ~$3/million input tokens, ~$15/million output tokens
- Claude 3 Haiku: ~$0.25/million input tokens, ~$1.25/million output tokens
- Direct billing from Anthropic
- Pay only for what you use
- Included in Workers Paid Plan: $5/month for 10M neurons
- Additional usage: $0.011 per 1,000 neurons
- No per-request API costs
- Predictable billing
- Free to use (no additional cost beyond underlying API)
- Reduces costs through caching
- Analytics included
- Supports multiple providers
Example Cost Scenarios (1M requests, 1000 tokens avg per request):
| Scenario | Simple Claude (Sonnet) | Workers AI | With AI Gateway (50% cache) |
|---|---|---|---|
| Input tokens | $3,000 | ~$11 | $1,500 |
| Output tokens | $15,000 | included | $7,500 |
| Total | $18,000 | ~$11 | ~$9,000 |
Note: Actual costs vary based on usage patterns, model choice, and caching effectiveness
- Node.js 18+
- Cloudflare account (free tier available)
- This boilerplate project setup
- Anthropic API key
- Set as
ANTHROPIC_API_KEYin Cloudflare Workers secrets
# Add API key as secret
npx wrangler secret put ANTHROPIC_API_KEY- Cloudflare Workers Paid plan ($5/month)
- AI binding in
wrangler.jsonc
- AI Gateway setup in Cloudflare dashboard
- Gateway endpoint URL
- Provider API keys (for providers you want to use)
Based on your requirements, navigate to the appropriate example directory.
Each example has a detailed README with:
- Setup instructions
- Configuration steps
- Integration guide
- Testing instructions
Each example includes a PRP.md (Product Requirement Plan) with:
- Complete implementation steps
- Code examples
- Validation steps
- Best practices
Follow the example's integration guide to add the code to your project.
All examples follow a similar pattern:
┌─────────────┐ ┌──────────────┐ ┌─────────────┐
│ React │─────▶│ Worker │─────▶│ AI API/ │
│ Component │◀─────│ /api/chat │◀─────│ Service │
└─────────────┘ └──────────────┘ └─────────────┘
(UI) (Middleware) (AI Provider)
- React Component: Handles UI and user interaction
- Worker Endpoint: Processes requests, calls AI API, handles errors
- AI Service: External API or Workers AI binding
# Use Cloudflare secrets for production
npx wrangler secret put ANTHROPIC_API_KEY
# Use .env.local for local development (gitignored)
echo "ANTHROPIC_API_KEY=sk-ant-..." > .dev.vars// Implement rate limiting in your worker
if (requestCount > RATE_LIMIT) {
return new Response('Rate limit exceeded', { status: 429 });
}// Always validate user input
if (!message || message.length > MAX_LENGTH) {
return Response.json({ error: 'Invalid input' }, { status: 400 });
}// Never expose internal errors to users
try {
const result = await ai.generate(prompt);
return Response.json(result);
} catch (error) {
console.error('AI error:', error);
return Response.json(
{ error: 'An error occurred processing your request' },
{ status: 500 }
);
}- Use AI Gateway for automatic caching
- Implement custom caching for repeated queries
- Cache static prompts and templates
- Workers AI runs at the edge (lowest latency)
- Cache frequently requested responses in KV
- Use Durable Objects for stateful conversations
- Batch requests when possible
- Use appropriate model sizes (Haiku vs Sonnet)
- Implement request deduplication
-
Local Development
npm run dev
-
Test with Local Wrangler
npx wrangler dev
-
Deploy to Production
npm run deploy
# Check if secret is set
npx wrangler secret list
# Add if missing
npx wrangler secret put ANTHROPIC_API_KEY// Add CORS headers in worker
return Response.json(data, {
headers: {
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'POST, OPTIONS',
'Access-Control-Allow-Headers': 'Content-Type',
}
});# Regenerate types after adding bindings
npm run cf-typegen- Check that your fetch call supports streaming
- Verify content-type is
text/event-stream - Ensure proper EventSource setup
- Choose an example that fits your needs
- Read the example's README thoroughly
- Follow the PRP for implementation
- Test locally before deploying
- Deploy to production
- Database integration examples (coming soon)
- Authentication examples (coming soon)
For issues specific to:
- These examples: Open an issue in the repository
- Cloudflare Workers: Check Cloudflare Community
- AI APIs: Consult provider documentation
These examples are part of the Cloudflare Workers + React boilerplate and follow the same license.
{ "ai": { "binding": "AI" } }