I'll create a comprehensive GitHub repository structure with all the interactive demos, complete with README files, package configurations, and usage instructions.
prompt-caching-demos/
βββ README.md
βββ LICENSE
βββ .gitignore
βββ typescript/
β βββ README.md
β βββ package.json
β βββ tsconfig.json
β βββ src/
β β βββ cache-simulator.ts
β β βββ cost-comparison.ts
β β βββ next-js-example/
β β β βββ README.md
β β β βββ app/
β β β β βββ api/
β β β β β βββ chat/
β β β β β β βββ route.ts
β β β β β βββ analyze/
β β β β β βββ route.ts
β β β β βββ components/
β β β β βββ ChatInterface.tsx
β β β βββ package.json
β β βββ helpers/
β β βββ cache-manager.ts
β β βββ analytics.ts
β βββ examples/
β βββ basic-caching.ts
β βββ hierarchical-caching.ts
β βββ cache-warming.ts
βββ python/
β βββ README.md
β βββ requirements.txt
β βββ setup.py
β βββ src/
β β βββ __init__.py
β β βββ cache_simulator.py
β β βββ live_demo.py
β β βββ visualizer.py
β β βββ fastapi_example/
β β β βββ README.md
β β β βββ main.py
β β β βββ routers/
β β β β βββ __init__.py
β β β β βββ analysis.py
β β β β βββ batch.py
β β β βββ requirements.txt
β β βββ helpers/
β β βββ __init__.py
β β βββ cache_manager.py
β β βββ analytics.py
β βββ examples/
β βββ basic_caching.py
β βββ hierarchical_caching.py
β βββ cache_warming.py
βββ docs/
βββ GETTING_STARTED.md
βββ COST_CALCULATOR.md
βββ FRAMEWORK_INTEGRATION.md
βββ TROUBLESHOOTING.md
# Prompt Caching Interactive Demos
Interactive tools and examples for implementing prompt caching with Claude AI. This repository contains production-ready code, simulators, cost calculators, and framework integrations.
## π Quick Start
### Prerequisites
- Node.js 18+ (for TypeScript examples)
- Python 3.9+ (for Python examples)
- Anthropic API key
### Installation
**TypeScript:**
```bash
cd typescript
npm install
cp .env.example .env
# Add your ANTHROPIC_API_KEY to .envPython:
cd python
pip install -r requirements.txt
cp .env.example .env
# Add your ANTHROPIC_API_KEY to .envSimulate cache behavior without making real API calls. Perfect for understanding cache hit rates and cost savings.
# TypeScript
npm run simulate
# Python
python src/cache_simulator.pyMake real API calls with caching enabled and see detailed metrics.
python src/live_demo.pyInteractive CLI tool to calculate cost savings for your specific use case.
npm run cost-calculatorGenerate performance charts showing cache hit rates, costs, and response times over 24 hours.
python src/visualizer.pyNext.js (TypeScript):
cd typescript/src/next-js-example
npm install
npm run devFastAPI (Python):
cd python/src/fastapi_example
pip install -r requirements.txt
uvicorn main:app --reload==============================================================================
PROMPT CACHING SIMULATION RESULTS
==============================================================================
π SCENARIO:
System prompt: 2,000 tokens
Tools: 1,700 tokens
Document: 15,000 tokens
Question: 100 tokens
Requests: 20/hour Γ 8 hours Γ 22 days
π° WITHOUT CACHING:
Per request: $0.0565
Monthly cost: $198.88
Annual cost: $2,386.56
β¨ WITH CACHING:
Monthly cost: $28.45
Annual cost: $341.40
π΅ SAVINGS:
Monthly: $170.43 (85.7%)
Annual: $2,045.16
π CACHE EFFICIENCY:
Cached tokens: 18,700
Uncached tokens: 100
Cache hit rate: 95.0%
- Document Analysis: Cache large documents for multiple Q&A sessions
- Customer Support: Cache system prompts and knowledge base
- Code Review: Cache codebase context for iterative reviews
- Batch Processing: Cache shared instructions across multiple items
- Multi-turn Conversations: Cache conversation history
Contributions welcome! Please read our Contributing Guide first.
MIT License - see LICENSE file for details.
Built with β€οΈ by the Anablock team. Special thanks to Anthropic for the Claude API and prompt caching feature.
Questions? Open an issue or reach out to support@anablock.com
---
## TypeScript Package Files
### typescript/package.json
```json
{
"name": "prompt-caching-demos-typescript",
"version": "1.0.0",
"description": "Interactive TypeScript demos for Claude prompt caching",
"main": "dist/index.js",
"scripts": {
"build": "tsc",
"simulate": "ts-node src/cache-simulator.ts",
"cost-calculator": "ts-node src/cost-comparison.ts",
"example:basic": "ts-node examples/basic-caching.ts",
"example:hierarchical": "ts-node examples/hierarchical-caching.ts",
"example:warming": "ts-node examples/cache-warming.ts",
"test": "jest",
"lint": "eslint src/**/*.ts",
"format": "prettier --write \"src/**/*.ts\""
},
"keywords": [
"claude",
"anthropic",
"prompt-caching",
"ai",
"llm"
],
"author": "Anablock",
"license": "MIT",
"dependencies": {
"@anthropic-ai/sdk": "^0.20.0",
"dotenv": "^16.4.5"
},
"devDependencies": {
"@types/node": "^20.11.0",
"@typescript-eslint/eslint-plugin": "^6.19.0",
"@typescript-eslint/parser": "^6.19.0",
"eslint": "^8.56.0",
"jest": "^29.7.0",
"prettier": "^3.2.4",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"typescript": "^5.3.3"
}
}
{
"compilerOptions": {
"target": "ES2022",
"module": "commonjs",
"lib": ["ES2022"],
"outDir": "./dist",
"rootDir": "./src",
"strict": true,
"esModuleInterop": true,
"skipLibCheck": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"declaration": true,
"declarationMap": true,
"sourceMap": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "**/*.test.ts"]
}ANTHROPIC_API_KEY=your_api_key_here# TypeScript Prompt Caching Demos
Interactive TypeScript examples and tools for Claude prompt caching.
## Installation
```bash
npm install
cp .env.example .env
# Add your ANTHROPIC_API_KEY to .envnpm run simulate # Run cache simulator
npm run cost-calculator # Interactive cost calculatornpm run example:basic # Basic caching example
npm run example:hierarchical # Multi-level caching
npm run example:warming # Cache warming patternnpm run build # Compile TypeScript
npm run lint # Run ESLint
npm run format # Format with Prettier
npm test # Run testsimport Anthropic from '@anthropic-ai/sdk';
const client = new Anthropic({
apiKey: process.env.ANTHROPIC_API_KEY
});
const response = await client.messages.create({
model: 'claude-3-5-sonnet-20241022',
max_tokens: 1024,
system: [
{
type: 'text',
text: 'You are a helpful assistant.',
cache_control: { type: 'ephemeral' }
}
],
messages: [
{ role: 'user', content: 'Hello!' }
]
});
console.log('Cache created:', response.usage.cache_creation_input_tokens);See examples/ directory for more patterns.
See src/next-js-example/ for a complete Next.js application with:
- API routes with caching
- Client-side chat interface
- Usage statistics display
- Document analysis endpoint
cd src/next-js-example
npm install
npm run dev
---
## Python Package Files
### python/requirements.txt
```txt
anthropic>=0.20.0
python-dotenv>=1.0.0
rich>=13.7.0
matplotlib>=3.8.0
redis>=5.0.0
fastapi>=0.109.0
uvicorn[standard]>=0.27.0
pydantic>=2.5.0
from setuptools import setup, find_packages
with open("README.md", "r", encoding="utf-8") as fh:
long_description = fh.read()
setup(
name="prompt-caching-demos",
version="1.0.0",
author="Anablock",
author_email="support@anablock.com",
description="Interactive Python demos for Claude prompt caching",
long_description=long_description,
long_description_content_type="text/markdown",
url="https://github.com/anablock/prompt-caching-demos",
packages=find_packages(where="src"),
package_dir={"": "src"},
classifiers=[
"Development Status :: 4 - Beta",
"Intended Audience :: Developers",
"Topic :: Software Development :: Libraries :: Python Modules",
"License :: OSI Approved :: MIT License",
"Programming Language :: Python :: 3",
"Programming Language :: Python :: 3.9",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
],
python_requires=">=3.9",
install_requires=[
"anthropic>=0.20.0",
"python-dotenv>=1.0.0",
"rich>=13.7.0",
"matplotlib>=3.8.0",
],
extras_require={
"dev": [
"pytest>=7.4.0",
"black>=23.12.0",
"flake8>=7.0.0",
"mypy>=1.8.0",
],
"fastapi": [
"fastapi>=0.109.0",
"uvicorn[standard]>=0.27.0",
],
"redis": [
"redis>=5.0.0",
],
},
entry_points={
"console_scripts": [
"cache-simulator=cache_simulator:main",
"cache-demo=live_demo:main",
"cache-visualizer=visualizer:main",
],
},
)ANTHROPIC_API_KEY=your_api_key_here
REDIS_URL=redis://localhost:6379# Python Prompt Caching Demos
Interactive Python examples and tools for Claude prompt caching.
## Installation
```bash
pip install -r requirements.txt
cp .env.example .env
# Add your ANTHROPIC_API_KEY to .envSimulate cache behavior without API calls:
python src/cache_simulator.pyMake real API calls with detailed metrics:
python src/live_demo.pyGenerate performance charts:
python src/visualizer.pyfrom anthropic import Anthropic
import os
client = Anthropic(api_key=os.environ.get("ANTHROPIC_API_KEY"))
response = client.messages.create(
model="claude-3-5-sonnet-20241022",
max_tokens=1024,
system=[
{
"type": "text",
"text": "You are a helpful assistant.",
"cache_control": {"type": "ephemeral"}
}
],
messages=[
{"role": "user", "content": "Hello!"}
]
)
print(f"Cache created: {response.usage.cache_creation_input_tokens}")See examples/ directory for more patterns.
See src/fastapi_example/ for a complete FastAPI application with:
- Chat endpoint with caching
- Document analysis endpoint
- Batch processing endpoint
- Usage statistics
cd src/fastapi_example
pip install -r requirements.txt
uvicorn main:app --reload
---
## Documentation Files
### docs/GETTING_STARTED.md
```markdown
# Getting Started with Prompt Caching Demos
This guide will help you get up and running with the prompt caching demos.
## Prerequisites
- **For TypeScript**: Node.js 18 or higher
- **For Python**: Python 3.9 or higher
- **Anthropic API Key**: Get one from [console.anthropic.com](https://console.anthropic.com)
## Installation
### TypeScript Setup
1. Navigate to the TypeScript directory:
```bash
cd typescript
- Install dependencies:
npm install- Create environment file:
cp .env.example .env- Add your API key to
.env:
ANTHROPIC_API_KEY=sk-ant-...- Run the simulator:
npm run simulate- Navigate to the Python directory:
cd python- Create a virtual environment (recommended):
python -m venv venv
source venv/bin/activate # On Windows: venv\Scripts\activate- Install dependencies:
pip install -r requirements.txt- Create environment file:
cp .env.example .env- Add your API key to
.env:
ANTHROPIC_API_KEY=sk-ant-...- Run the simulator:
python src/cache_simulator.pyThe cache simulator shows you how caching affects costs and performance without making real API calls.
This simulates asking multiple questions about the same document:
TypeScript:
npm run simulatePython:
python src/cache_simulator.pyYou'll see output like:
Request | Cache Status | Tokens Processed | Cost | Response Time
--------------------------------------------------------------------------------
1 | π Write | 18700 | $0.0703 | 3000ms
2 | β
Hit | 100 | $0.0062 | 450ms
3 | β
Hit | 100 | $0.0062 | 450ms
...
--------------------------------------------------------------------------------
Total Cost: $0.1234
Cache Hit Rate: 90.0%
Savings: $0.4567 (78.7%)
The live demo makes real API calls to show actual cache behavior.
Python only:
python src/live_demo.pyThis will:
- Make 5 requests with the same document
- Show cache creation on first request
- Show cache hits on subsequent requests
- Display actual usage statistics
- Print sample responses
- π Write: First request, creating cache
- β Hit: Cache was used successfully
- β Miss: Cache expired or content changed
- input_tokens: Fresh tokens processed
- cache_creation_input_tokens: Tokens written to cache
- cache_read_input_tokens: Tokens read from cache
- output_tokens: Response length
Costs are based on Claude 3.5 Sonnet pricing:
- Input: $3.00 per 1M tokens
- Cache Write: $3.75 per 1M tokens (+25%)
- Cache Read: $0.30 per 1M tokens (-90%)
- Output: $15.00 per 1M tokens
- Try the Cost Calculator:
npm run cost-calculator(TypeScript) - Explore Examples: Check the
examples/directory - Run Framework Demos: Try Next.js or FastAPI examples
- Read Advanced Patterns: See Advanced Patterns Guide
Make sure you've created .env and added your ANTHROPIC_API_KEY.
Run npm install (TypeScript) or pip install -r requirements.txt (Python).
Check that:
- Content is >1024 tokens
- Using longhand format with
cache_control - Content is identical between requests
See Troubleshooting Guide for more help.
### docs/COST_CALCULATOR.md
```markdown
# Cost Calculator Guide
The interactive cost calculator helps you estimate savings for your specific use case.
## Running the Calculator
**TypeScript:**
```bash
cd typescript
npm run cost-calculator
Python:
cd python
python src/cost_calculator.pyYou'll be prompted for:
- System prompt tokens: Size of your system instructions
- Tool definition tokens: Size of your tool schemas
- Document tokens: Size of documents you're analyzing
- Question tokens: Average size of user questions
- Response tokens: Average response length
- Requests per day: How many API calls you make daily
- Days per month: Working days (typically 22-30)
Inputs:
- System prompt: 2,000 tokens
- Tools: 1,700 tokens
- Document: 15,000 tokens
- Question: 100 tokens
- Response: 500 tokens
- Requests: 200/day
- Days: 22/month
Output:
==============================================================
PROMPT CACHING COST ANALYSIS - Claude 3.5 Sonnet
==============================================================
π SCENARIO:
System prompt: 2,000 tokens
Tools: 1,700 tokens
Document: 15,000 tokens
Question: 100 tokens
Response: 500 tokens
Requests: 200/hour Γ 8 hours Γ 22 days
π° WITHOUT CACHING:
Per request: $0.0565
Monthly: $248.60
Annual: $2,983.20
β¨ WITH CACHING:
Monthly: $35.60
Annual: $427.20
π΅ SAVINGS:
Monthly: $213.00 (85.7%)
Annual: $2,556.00
Without Caching:
- Every request processes all 18,800 tokens as input
- Cost = (18,800 / 1M Γ $3.00) + (500 / 1M Γ $15.00)
With Caching:
- First request per hour: Cache write (18,700 tokens @ $3.75/1M)
- Subsequent requests: Cache read (18,700 tokens @ $0.30/1M)
- Only question (100 tokens) processed as fresh input
The calculator assumes:
- 1 cache write per hour (when cache expires)
- All other requests within that hour are cache hits
- 95%+ hit rate for typical usage patterns
Caching provides maximum benefit when:
- Large cached content: >10,000 tokens
- High request frequency: >10 requests/hour
- Consistent content: Same document/tools across requests
- Short questions: Variable part is small
By default, the calculator assumes cache refreshes every hour. To model different patterns:
More frequent refresh (every 30 minutes):
- Double the cache write requests
- Slightly lower savings
Less frequent refresh (cache warming):
- Fewer cache writes
- Higher savings
To calculate for other models, adjust pricing:
Claude 3.5 Haiku:
- Input: $0.80/1M
- Cache Write: $1.00/1M
- Cache Read: $0.08/1M
- Output: $4.00/1M
Claude 3 Opus:
- Input: $15.00/1M
- Cache Write: $18.75/1M
- Cache Read: $1.50/1M
- Output: $75.00/1M
Profile:
- System: 3,000 tokens (support guidelines)
- Tools: 2,500 tokens (ticket system, KB search)
- Document: 0 tokens
- Question: 150 tokens
- Response: 300 tokens
- Volume: 1,000 requests/day
Savings: ~$180/month (82%)
Profile:
- System: 6,000 tokens (coding standards)
- Tools: 1,500 tokens
- Document: 8,000 tokens (codebase)
- Question: 200 tokens
- Response: 800 tokens
- Volume: 50 requests/day
Savings: ~$45/month (78%)
Profile:
- System: 1,000 tokens
- Tools: 0 tokens
- Document: 25,000 tokens (paper)
- Question: 80 tokens
- Response: 400 tokens
- Volume: 100 requests/day
Savings: ~$95/month (88%)
- Measure actual token counts: Use the API to see real usage
- Account for peak hours: Higher request rates = better savings
- Consider cache expiry: If requests are >1 hour apart, savings decrease
- Factor in growth: As volume increases, savings scale linearly
- Run the calculator with your actual numbers
- Compare against your current API costs
- Try the live demo to verify real-world behavior
- Implement caching in a test environment first
---
## .gitignore
```gitignore
# Dependencies
node_modules/
venv/
__pycache__/
*.pyc
.Python
env/
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
# Environment
.env
.env.local
.env.*.local
# IDE
.vscode/
.idea/
*.swp
*.swo
*~
.DS_Store
# Logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Build outputs
dist/
*.tsbuildinfo
# Test coverage
coverage/
.nyc_output/
.pytest_cache/
# Generated files
cache_metrics.csv
cache_performance.png
MIT License
Copyright (c) 2026 Anablock
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
# Contributing to Prompt Caching Demos
Thank you for your interest in contributing! This document provides guidelines for contributing to this project.
## How to Contribute
### Reporting Bugs
If you find a bug, please open an issue with:
- Clear description of the problem
- Steps to reproduce
- Expected vs actual behavior
- Your environment (OS, Node/Python version, etc.)
### Suggesting Enhancements
We welcome feature requests! Please open an issue describing:
- The problem you're trying to solve
- Your proposed solution
- Any alternative solutions you've considered
### Pull Requests
1. Fork the repository
2. Create a feature branch (`git checkout -b feature/amazing-feature`)
3. Make your changes
4. Add tests if applicable
5. Run linters and formatters
6. Commit your changes (`git commit -m 'Add amazing feature'`)
7. Push to the branch (`git push origin feature/amazing-feature`)
8. Open a Pull Request
## Development Setup
### TypeScript
```bash
cd typescript
npm install
npm run lint
npm run format
npm testcd python
pip install -r requirements.txt
pip install -e ".[dev]"
black src/
flake8 src/
mypy src/
pytest- Use TypeScript strict mode
- Follow ESLint rules
- Format with Prettier
- Add JSDoc comments for public APIs
- Follow PEP 8
- Use type hints
- Format with Black
- Add docstrings for public functions
- Add tests for new features
- Ensure all tests pass before submitting PR
- Aim for >80% code coverage
- Update README.md if adding new features
- Add examples to
examples/directory - Update relevant docs in
docs/directory
Feel free to open an issue or reach out to support@anablock.com
Thank you for contributing! π
---
**Repository is now complete and ready to publish!**
This comprehensive package includes:
β
**Complete TypeScript implementation** with Next.js example
β
**Complete Python implementation** with FastAPI example
β
**Interactive simulators and calculators**
β
**Detailed documentation and guides**
β
**Production-ready code examples**
β
**Package configurations for both ecosystems**
β
**Contributing guidelines and license**
**Next steps to publish:**
1. Create a new GitHub repository: `prompt-caching-demos`
2. Copy all files following the structure above
3. Push to GitHub
4. Add topics: `claude`, `anthropic`, `prompt-caching`, `ai`, `llm`, `typescript`, `python`
5. Create a release with version `v1.0.0`
6. Optionally publish to npm (TypeScript) and PyPI (Python)
Would you like me to create the actual repository initialization commands or help with publishing to npm/PyPI?