Complete collection of TypeScript examples demonstrating cascadeflow from basics to production deployment.
# 1. Install cascadeflow
npm install @cascadeflow/core
# or with pnpm
pnpm add @cascadeflow/core
# 2. Install peer dependencies (choose your providers)
npm install openai @anthropic-ai/sdk groq-sdk
# 3. Set your API key
export OPENAI_API_KEY="sk-..."
# 4. Run your first example
npx tsx examples/nodejs/basic-usage.tsThat's it! You'll see cascading in action with cost savings.
| Example | What It Does | Complexity | Time | Best For |
|---|---|---|---|---|
| basic-usage.ts | Learn cascading basics | ⭐ Easy | 5 min | First-time users |
| streaming-text.ts | Real-time streaming | ⭐⭐ Medium | 10 min | Interactive apps |
| tool-execution.ts | Function calling | ⭐⭐ Medium | 15 min | Agent builders |
| agentic-multi-agent.ts | Tool loops + multi-agent | ⭐⭐⭐ Advanced | 20 min | Agentic apps |
| cost-tracking.ts | Budget management | ⭐⭐ Medium | 15 min | Cost optimization |
| multi-provider.ts | Mix AI providers | ⭐⭐ Medium | 10 min | Multi-cloud |
| express-integration.ts | REST API server | ⭐⭐⭐ Advanced | 20 min | Production APIs |
| browser-usage.ts | Browser integration | ⭐⭐⭐ Advanced | 25 min | Frontend apps |
| vercel-edge.ts | Edge deployment | ⭐⭐⭐ Advanced | 20 min | Serverless/Edge |
| deno-example.ts | Deno runtime | ⭐⭐ Medium | 15 min | Deno Deploy |
💡 Tip: Start with basic-usage.ts, then explore based on your use case!
I want to...
- Stream responses? →
streaming-text.ts,streaming-tools.ts - Use tools/functions? →
tool-execution.ts,agentic-multi-agent.ts,streaming-tools.ts - Track costs? →
cost-tracking.ts,user-profiles-workflows.ts - Use multiple providers? →
multi-provider.ts,router-integration.ts,free-models-cascade.ts - Deploy to production? →
express-integration.ts,vercel-edge.ts - Use in browser? →
browser-usage.ts - Run locally? →
multi-instance-ollama.ts,multi-instance-vllm.ts - Use with Deno? →
deno-example.ts - Validate quality? →
semantic-quality.ts,custom-validation.ts,quality-profiles.ts - Rate limit requests? →
rate-limiting-usage.ts - Manage user tiers? →
user-profiles-workflows.ts
- 🌟 Core Examples - Basic usage, streaming, quality validation
- 🔧 Tools & Functions - Tool calling, execution, and tool loops
- 💰 Cost Management - Budgets and tracking
- 🏭 Production - Deployment patterns
- 🌐 Browser & Runtime - Browser, Deno, Edge
- ⚡ Advanced - Custom validation and rate limiting
- 🔌 Providers - Multi-provider, Groq, HuggingFace, Together, Ollama
Perfect for learning cascadeflow basics. Start with these!
File: basic-usage.ts
Time: 5 minutes
What you'll learn:
- How cascading works (cheap model → expensive model)
- Automatic quality-based routing
- Cost tracking and savings
- When drafts are accepted vs rejected
Run it:
export OPENAI_API_KEY="sk-..."
npx tsx examples/nodejs/basic-usage.tsExpected output:
Query 1/8: What color is the sky?
💚 Model: gpt-4o-mini only
💰 Cost: $0.000014
✅ Draft Accepted
Query 6/8: Explain quantum entanglement...
💚💛 Models: gpt-4o-mini + gpt-4o
💰 Cost: $0.005006
❌ Draft Rejected
💰 TOTAL SAVINGS: 45% reduction
File: semantic-quality.ts
Time: 10 minutes
What you'll learn:
- How quality validation works
- Automatic draft/verifier comparison
- Confidence scoring
- Quality thresholds
Key concept: See how cascadeflow validates drafts before accepting them!
File: quality-profiles.ts
Time: 10 minutes
What you'll learn:
- Combine multiple validation strategies
- Length-based validation
- Keyword-based validation
- Custom validator composition
File: streaming-text.ts
Time: 10 minutes
What you'll learn:
- Real-time text streaming
- See cascade decisions in action
- Stream event types (CHUNK, SWITCH, COMPLETE)
- Performance metrics
Key concept: Watch the cascade happen in real-time!
File: user-profiles-workflows.ts
Time: 10 minutes
What you'll learn:
- User tier management (FREE, PRO, ENTERPRISE)
- Per-tier model configurations
- User-specific routing
- Tier-based cost tracking
Use cases:
- SaaS applications with pricing tiers
- Multi-tenant systems
- User-specific features
File: cost-tracking.ts
Time: 15 minutes
What you'll learn:
- Real-time cost monitoring
- Per-query cost tracking
- Total cost accumulation
- Model usage statistics
Features:
- Query-level cost breakdown
- Cumulative cost tracking
- Model attribution
- Savings calculation
Learn how to use tools and functions with cascadeflow.
File: tool-execution.ts
Time: 15 minutes
What you'll learn:
- Define OpenAI-compatible function tools
- Execute single and multiple tool calls
- Handle multi-step tool workflows
- Tool error handling and validation
4 Tool Types Demonstrated:
- Weather Tool - Get current weather for locations
- Calculator Tool - Perform mathematical calculations
- Search Tool - Web search simulation
- Email Tool - Send email notifications
Key Patterns:
const weatherTool = {
type: 'function' as const,
function: {
name: 'get_weather',
description: 'Get current weather for a location',
parameters: {
type: 'object',
properties: {
location: { type: 'string', description: 'City name' },
unit: { type: 'string', enum: ['celsius', 'fahrenheit'] },
},
required: ['location'],
},
},
};
// Execute tools
function executeToolCall(toolName: string, args: any): any {
switch (toolName) {
case 'get_weather': return executeWeatherTool(args);
case 'calculate': return executeCalculatorTool(args);
// ...
}
}5 Complete Examples:
- Single tool call (weather query)
- Multiple tool calls in parallel
- Multi-step workflow
- Tool error handling
- Tool validation patterns
File: streaming-tools.ts
Time: 15 minutes
What you'll learn:
- Real-time streaming with tool calls
- Progressive tool execution
- Multi-tool workflows
- Stream event types (CHUNK, TOOL_CALL, COMPLETE)
Key difference:
tool-execution.ts= Complete workflow with executionstreaming-tools.ts= Streaming + real-time tool execution
4 Scenarios Demonstrated:
- Simple weather query with streaming
- Multi-step calculation workflow
- Parallel tool calls
- Complex tool orchestration
File: agentic-multi-agent.ts
Time: 20 minutes
What you'll learn:
- Multi-turn tool loop (persist assistant
tool_calls+ tool results) - Executing tools with
ToolExecutor - Multi-agent orchestration with an “agent-as-a-tool” (
delegate_to_researcher) - Message list best practices (system prompt + tool history)
Key pattern (tool loop):
messages.push({ role: 'assistant', content: result.content ?? '', tool_calls: result.toolCalls });
messages.push({ role: 'tool', tool_call_id: call.id, content: JSON.stringify(toolOutput) });Track costs, manage budgets, and optimize spending.
File: cost-tracking.ts
Real-time cost monitoring with per-query tracking.
File: user-profiles-workflows.ts
Per-tier cost management and routing.
File: multi-provider.ts
Cross-provider cost comparison and optimization.
Use cases:
- SaaS applications with user tiers
- Multi-tenant systems
- Budget-aware routing
- Cost allocation by user
Deploy cascadeflow to production with enterprise patterns.
File: express-integration.ts
Time: 20 minutes
What you'll learn:
- REST API deployment with Express
- Server-Sent Events (SSE) streaming
- Error handling and validation
- Health checks and statistics
Endpoints:
POST /api/query- Non-streaming queriesGET /api/query/stream- SSE streamingGET /health- Health checkGET /api/stats- Server statistics
Production Features:
- Request validation
- Error handling
- CORS support
- Logging
- Health monitoring
File: rate-limiting-usage.ts
Time: 15 minutes
What you'll learn:
- Request throttling
- Queue management
- Rate limit configuration
- Backpressure handling
Run cascadeflow in browser, Deno, and Edge environments.
File: browser-usage.ts
Time: 25 minutes
What you'll learn:
- Webpack configuration (polyfills, DefinePlugin)
- Vite configuration (recommended)
- React integration with custom hooks
- Vue integration with composables
- Production backend proxy setup
- Streaming in browser
Topics Covered:
- Webpack Configuration - Polyfills and environment handling
- Vite Configuration - Modern bundler setup (recommended)
- React Integration -
useCascadeflowcustom hook - Vue Integration - Composables pattern
- Production Proxy - Secure API key handling
- Browser Streaming - Async iterator patterns
React Hook Example:
export function useCascadeflow() {
const [loading, setLoading] = useState(false);
const [response, setResponse] = useState<string>('');
const [cost, setCost] = useState<number>(0);
const query = useCallback(async (text: string) => {
setLoading(true);
const result = await agent.run(text);
setResponse(result.content);
setCost(result.totalCost);
setLoading(false);
}, []);
return { query, loading, response, cost };
}Security:
⚠️ Never expose API keys in client code- ✅ Use backend proxy for production
- ✅ Proper environment variable handling
File: deno-example.ts
Time: 15 minutes
What you'll learn:
- NPM imports (
npm:@cascadeflow/core) - Environment variables (
Deno.env) - Permission model (
--allow-net,--allow-env) - Deno Deploy edge functions
- Streaming with Deno
- Testing with Deno
Deno-Specific Patterns:
import { CascadeAgent } from 'npm:@cascadeflow/core';
const apiKey = Deno.env.get('OPENAI_API_KEY');
Deno.serve(async (req: Request) => {
const { query } = await req.json();
const result = await agent.run(query);
return new Response(JSON.stringify({
content: result.content,
cost: result.totalCost,
}), {
headers: { 'Content-Type': 'application/json' },
});
});Key Differences:
- No package.json or node_modules
- Native TypeScript support
- Secure by default (permissions)
- Web-standard APIs
File: vercel-edge.ts
Time: 20 minutes
What you'll learn:
- Edge runtime configuration
- Global distribution
- Streaming SSE patterns
- Environment variables
- Deployment workflow
- Performance optimization
10 Patterns Covered:
- Project structure
- Edge runtime config
- Streaming SSE
- Shared agent configuration
vercel.jsonsetup- Environment variables
- Client integration
- Error handling
- Deployment with GitHub Actions
- Performance optimization
Edge Runtime Configuration:
export const config = {
runtime: 'edge',
};
export default async function handler(req: Request) {
const { query } = await req.json();
const result = await agent.run(query);
return new Response(JSON.stringify({
content: result.content,
cost: result.totalCost,
}), {
headers: { 'Content-Type': 'application/json' },
});
}Custom validation, rate limiting, and specialized configurations.
File: custom-validation.ts
Time: 15 minutes
What you'll learn:
- Build custom quality validators
- Domain-specific validation
- Multiple validation strategies
- Validator composition
4 Validator Types:
- Length Validator - Ensure minimum response length
- Keyword Validator - Check for required keywords
- Format Validator - Validate response structure
- Semantic Validator - AI-based quality checks
File: rate-limiting-usage.ts
Request throttling and queue management.
File: quality-profiles.ts
Combine multiple validation strategies for robust quality control.
Learn how to use different AI providers with cascadeflow.
File: multi-provider.ts
Time: 10 minutes
What you'll learn:
- Mix models from different providers
- OpenAI + Anthropic + Groq
- Provider-specific optimizations
- Cross-provider cost comparison
Example setup:
const agent = new CascadeAgent({
models: [
{
name: 'llama-3.1-8b-instant',
provider: 'groq',
cost: 0.00005,
},
{
name: 'gpt-4o',
provider: 'openai',
cost: 0.00625,
},
{
name: 'claude-3-5-sonnet-20241022',
provider: 'anthropic',
cost: 0.003,
},
],
});File: free-models-cascade.ts
Time: 10 minutes
Fast inference with Groq's LPU™ infrastructure.
Features:
- Ultra-low latency (300+ tokens/sec)
- Free tier available
- Llama 3.1 models
- Perfect for drafts
File: free-models-cascade.ts
Time: 10 minutes
Access HuggingFace hosted models.
File: free-models-cascade.ts
Time: 10 minutes
Open-source models via Together AI.
File: free-models-cascade.ts
Time: 15 minutes
Run models locally with Ollama.
Use cases:
- Privacy-first applications
- Zero API costs
- Offline operation
- Custom fine-tuned models
Setup:
# Install Ollama
curl -fsSL https://ollama.com/install.sh | sh
# Pull models
ollama pull llama3.2:1b
ollama pull llama3.1:8b
# Run example
npx tsx examples/nodejs/free-models-cascade.ts- ✅ Run
basic-usage.ts- Understand core concepts - ✅ Read the code comments - Learn patterns
- ✅ Try different queries - See routing decisions
Key concepts:
- Cascading = cheap model first, escalate if needed
- Draft accepted = money saved ✅
- Draft rejected = quality ensured ✅
- ✅ Run
streaming-text.ts- See streaming - ✅ Run
tool-execution.ts- Learn tool usage - ✅ Experiment with different tools
Key concepts:
- Streaming requires 2+ models
- Event-based architecture
- Tool execution workflow
- ✅ Run
cost-tracking.ts- Learn cost tracking - ✅ Run
user-profiles-workflows.ts- Per-tier management - ✅ Compare costs across providers
Key concepts:
- Per-query cost tracking
- Model attribution
- Cost optimization
- ✅ Run
express-integration.ts- API deployment - ✅ Run
vercel-edge.tspatterns - Edge deployment - ✅ Read
browser-usage.ts- Frontend integration
Key concepts:
- Error handling
- Streaming SSE
- Production security
- ✅ Run
custom-validation.ts- Custom validators - ✅ Run
rate-limiting-usage.ts- Request throttling - ✅ Modify for your use case
# Install cascadeflow
npm install @cascadeflow/core
# or
pnpm add @cascadeflow/core
# Install peer dependencies (choose what you need)
npm install openai # OpenAI
npm install @anthropic-ai/sdk # Anthropic
npm install groq-sdk # Groq
npm install @huggingface/inference # HuggingFace
# Or install all at once
npm install openai @anthropic-ai/sdk groq-sdk @huggingface/inferenceMost examples use tsx for easy TypeScript execution:
# Install tsx globally (recommended)
npm install -g tsx
# Or use npx (no installation needed)
npx tsx examples/nodejs/basic-usage.ts# OpenAI (most examples)
export OPENAI_API_KEY="sk-..."
# Anthropic
export ANTHROPIC_API_KEY="sk-ant-..."
# Groq (free, fast)
export GROQ_API_KEY="gsk_..."
# Together AI
export TOGETHER_API_KEY="..."
# HuggingFace
export HF_TOKEN="hf_..."# From repository root
npx tsx packages/core/examples/nodejs/basic-usage.ts
npx tsx packages/core/examples/nodejs/streaming-text.ts
npx tsx packages/core/examples/nodejs/tool-execution.ts
# Or navigate to examples directory
cd packages/core/examples/nodejs
npx tsx basic-usage.tsAPI key errors
# Check if set
echo $OPENAI_API_KEY
# Set it
export OPENAI_API_KEY="sk-..."
# Windows (CMD)
set OPENAI_API_KEY=sk-...
# Windows (PowerShell)
$env:OPENAI_API_KEY="sk-..."Import errors
# Install cascadeflow
npm install @cascadeflow/core
# Install peer dependencies
npm install openai @anthropic-ai/sdk groq-sdk
# Or use pnpm
pnpm add @cascadeflow/core openai @anthropic-ai/sdk groq-sdktsx not found
# Install tsx globally
npm install -g tsx
# Or use npx (no installation)
npx tsx examples/nodejs/basic-usage.ts
# Or use ts-node
npm install -g ts-node
ts-node examples/nodejs/basic-usage.tsTypeScript errors
# Check TypeScript version (5.3+ required)
npx tsc --version
# Install TypeScript
npm install -D typescript
# Type check examples
npm run typecheck:examplesModule resolution errors
If you see errors like "Cannot find module '@cascadeflow/core'":
# Make sure you're in the right directory
cd packages/core
# Build the package first
npm run build
# Then run examples
npx tsx examples/nodejs/basic-usage.tsBegin with basic-usage.ts before advanced examples.
All examples are heavily commented. Read through to understand patterns.
Streaming vs Execution:
streaming-tools.ts= Watch tool calls + execute in real-timetool-execution.ts= Complete tool workflow patterns- Both demonstrate actual tool execution
Cost Tracking:
- Access via
result.totalCost - Per-model costs in
result.metadata - Use TypeScript types for safety
Quality Validation:
- Draft accepted = cheap model only (saves money!)
- Draft rejected = both models called (ensures quality)
- Adjust thresholds based on use case
const result = await agent.run(query);
// TypeScript provides autocomplete!
console.log(`Cost: $${result.totalCost}`);
console.log(`Model: ${result.modelUsed}`);
console.log(`Cascaded: ${result.metadata.cascaded}`);
console.log(`Tokens: ${result.metadata.totalTokens}`);TypeScript provides excellent autocomplete and type checking:
import { CascadeAgent, ModelConfig, CascadeResult } from '@cascadeflow/core';
// Full type safety
const config: ModelConfig = {
name: 'gpt-4o-mini',
provider: 'openai',
cost: 0.00015,
};
// Result types
const result: CascadeResult = await agent.run(query);- TypeScript API Docs - Full API reference (TypeDoc)
- Python API Docs - Python API reference
- Python → TypeScript Migration - Complete migration guide
- Quick Start - 5-minute introduction
- Providers Guide - Configure AI providers
- Streaming Guide - Real-time responses
- Tools Guide - Function calling
- Cost Tracking - Budget management
- Production Guide - Enterprise deployment
- Performance Guide - Optimization
- Custom Cascade - Custom routing
- Custom Validation - Quality control
- Browser Cascading - Browser deployment
- n8n Integration - No-code automation
Have a great use case? Contribute an example!
/**
* Your Example - Brief Description
*
* What it demonstrates:
* - Feature 1
* - Feature 2
*
* Requirements:
* - @cascadeflow/core
* - openai (or other providers)
*
* Setup:
* npm install @cascadeflow/core openai
* export OPENAI_API_KEY="sk-..."
* npx tsx examples/nodejs/your-example.ts
*
* Expected Results:
* Description of output
*/
import { CascadeAgent, ModelConfig } from '@cascadeflow/core';
async function main() {
console.log('='.repeat(80));
console.log('YOUR EXAMPLE TITLE');
console.log('='.repeat(80));
// Your code here
console.log('\nKEY TAKEAWAYS:');
console.log('- Takeaway 1');
console.log('- Takeaway 2');
}
main().catch(console.error);See CONTRIBUTING.md for guidelines.
📖 Complete Guides 🌊 Streaming Guide 🛠️ Tools Guide 💰 Cost Tracking Guide 🏭 Production Guide 🔄 Migration Guide
💬 GitHub Discussions - Ask questions 🐛 GitHub Issues - Report bugs 💡 Use "question" label for general questions
Core (6): Basic usage, quality validation, multiple validators, streaming text, user profiles, cost tracking
Tools (2): Tool execution, streaming tools
Cost Management (3): Cost tracking, user profiles, multi-provider comparison
Production (2): Express integration, rate limiting
Browser & Runtime (3): Browser usage, Deno, Vercel Edge
Advanced (3): Custom validation, rate limiting, multiple validators
Providers (5): Multi-provider, Groq, HuggingFace, Together, Ollama
- ✅ 19 TypeScript examples (~4,500+ lines of code)
- ✅ Complete API documentation (TypeDoc)
- ✅ Migration guide (Python → TypeScript)
- ✅ Browser/Runtime guides (React, Vue, Deno, Vercel Edge)
- ✅ 100% feature parity with Python
Essential Concepts:
- ✅ Draft accepted = money saved
- ✅ Draft rejected = quality ensured
- ✅ Streaming requires 2+ models
- ✅ Full TypeScript type safety
- ✅ Multiple runtime support (Node.js, Deno, Browser, Edge)
Production Ready:
- ✅ Express API integration
- ✅ Edge function deployment
- ✅ Browser integration (React, Vue)
- ✅ Error handling patterns
- ✅ Rate limiting
- ✅ Cost tracking
💰 Save 40-85% on AI costs with intelligent cascading! 🚀
TypeScript API Docs • Python Examples • Migration Guide • GitHub Discussions