Skip to content

Commit af2521f

Browse files
Add prompt-caching examples for fetch API (#42)
Add prompt-caching examples for fetch API - Add comprehensive documentation in docs/prompt-caching.md - Add 4 separate example files in typescript/fetch/src/prompt-caching/: - system-message-cache.ts: Cache on system message - user-message-cache.ts: Cache on user message - multi-message-cache.ts: Cache in multi-turn conversation - no-cache-control.ts: Control scenario (no caching) - Add shared workspace with LARGE_SYSTEM_PROMPT constant - Add Bun monorepo structure with workspaces - Add Makefile for build orchestration - Add biome.jsonc for code quality Run biome format on prompt-caching examples Simplify examples section to link only to folder Simplify fetch prompt-caching README to link to main docs Rename prompt caching examples with anthropic prefix
1 parent e69aba6 commit af2521f

19 files changed

+1265
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
node_modules/
2+
typescript/node_modules/
3+
typescript/*/node_modules/
4+
typescript/bun.lock
5+
*.log
6+
.DS_Store

Makefile

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Makefile - Root orchestration for openrouter-examples
2+
3+
.PHONY: help examples typescript install clean
4+
5+
help:
6+
@echo "OpenRouter Examples - Available commands:"
7+
@echo ""
8+
@echo " make examples - Run all examples"
9+
@echo " make typescript - Run TypeScript monorepo examples"
10+
@echo " make install - Install TypeScript dependencies"
11+
@echo " make clean - Clean node_modules and lockfiles"
12+
@echo ""
13+
14+
# Run all examples
15+
examples: typescript
16+
17+
# Run TypeScript monorepo examples
18+
typescript:
19+
@echo "=== Running TypeScript examples ==="
20+
@cd typescript && bun examples
21+
22+
# Install TypeScript dependencies
23+
install:
24+
@echo "=== Installing TypeScript dependencies ==="
25+
@cd typescript && bun install
26+
27+
# Clean build artifacts
28+
clean:
29+
@echo "=== Cleaning TypeScript artifacts ==="
30+
@rm -rf typescript/node_modules
31+
@rm -rf typescript/*/node_modules
32+
@rm -rf typescript/bun.lock
33+
@echo "Clean complete"

README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# OpenRouter Examples
2+
3+
Comprehensive, tested, executable examples demonstrating OpenRouter features across multiple ecosystems.
4+
5+
## Quick Start
6+
7+
```bash
8+
# Set your API key
9+
export OPENROUTER_API_KEY="your-key-here"
10+
11+
# Run all examples
12+
make examples
13+
14+
# Or run specific ecosystems
15+
make curl # Run curl examples
16+
make typescript # Run TypeScript monorepo examples
17+
```
18+
19+
## Repository Structure
20+
21+
```
22+
.
23+
├── curl/ - Shell script examples
24+
├── typescript/ - TypeScript monorepo (Bun workspaces)
25+
│ ├── shared/ - Shared constants and types
26+
│ ├── fetch/ - Raw fetch API examples
27+
│ ├── ai-sdk-v5/ - Vercel AI SDK v5 examples
28+
│ ├── effect-ai/ - Effect-TS examples
29+
│ └── openrouter-sdk/ - OpenRouter SDK examples (TODO)
30+
├── docs/ - Feature documentation
31+
└── Makefile - Unified command interface
32+
```
33+
34+
## Features
35+
36+
### Prompt Caching
37+
- **Documentation**: [docs/prompt-caching.md](docs/prompt-caching.md)
38+
- **Examples**:
39+
- [curl/prompt-caching.sh](curl/prompt-caching.sh)
40+
- [typescript/fetch/src/prompt-caching/](typescript/fetch/src/prompt-caching/)
41+
- [typescript/ai-sdk-v5/src/prompt-caching/](typescript/ai-sdk-v5/src/prompt-caching/)
42+
- [typescript/effect-ai/src/prompt-caching/](typescript/effect-ai/src/prompt-caching/)
43+
44+
## Prerequisites
45+
46+
- Bun runtime: `curl -fsSL https://bun.sh/install | bash`
47+
- OpenRouter API key: [https://openrouter.ai/keys](https://openrouter.ai/keys)
48+
- For curl examples: `jq` (JSON processor)
49+
50+
## Installation
51+
52+
```bash
53+
# Install TypeScript dependencies
54+
make install
55+
56+
# Or manually
57+
cd typescript && bun install
58+
```
59+
60+
## Running Examples
61+
62+
### All Examples
63+
```bash
64+
make examples
65+
```
66+
67+
### By Ecosystem
68+
```bash
69+
make curl # Shell scripts with curl + jq
70+
make typescript # All TypeScript examples (fetch, AI SDK, Effect)
71+
```
72+
73+
### Individual Examples
74+
```bash
75+
# curl
76+
bash curl/prompt-caching.sh
77+
78+
# TypeScript
79+
cd typescript/fetch && bun examples
80+
cd typescript/ai-sdk-v5 && bun examples
81+
cd typescript/effect-ai && bun examples
82+
```
83+
84+
## Benefits
85+
86+
### For Users
87+
1. **Copy-paste ready** - All examples are runnable as-is
88+
2. **Tested and proven** - Every example has been verified to work
89+
3. **Evidence-based** - Examples show expected outputs and verification
90+
4. **Multiple ecosystems** - Choose the one that matches your stack
91+
92+
### For Developers
93+
1. **Single source of truth** - Constants defined once in `typescript/shared/`
94+
2. **Type safety** - Shared types across all TypeScript examples
95+
3. **Consistent patterns** - Each ecosystem follows its own idioms
96+
4. **Easy maintenance** - Bun monorepo for TypeScript workspaces
97+
98+
## Contributing
99+
100+
See individual ecosystem READMEs:
101+
- [curl/README.md](curl/README.md)
102+
- [typescript/README.md](typescript/README.md)
103+
104+
## License
105+
106+
See [LICENSE.md](LICENSE.md)

biome.jsonc

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
{
2+
"$schema": "https://biomejs.dev/schemas/2.3.3/schema.json",
3+
"vcs": {
4+
"enabled": true,
5+
"clientKind": "git",
6+
"useIgnoreFile": true
7+
},
8+
"files": {
9+
"ignoreUnknown": false,
10+
"ignore": [
11+
"**/*.json",
12+
"!biome.json"
13+
]
14+
},
15+
"formatter": {
16+
"enabled": true,
17+
"indentStyle": "space",
18+
"indentWidth": 2,
19+
"lineWidth": 100,
20+
"attributePosition": "multiline"
21+
},
22+
"organizeImports": {
23+
"enabled": true
24+
},
25+
"linter": {
26+
"enabled": true,
27+
"rules": {
28+
"recommended": true,
29+
"complexity": {
30+
"useLiteralKeys": "off",
31+
"noExtraBooleanCast": "off",
32+
"noForEach": "off",
33+
"noBannedTypes": "error",
34+
"noUselessSwitchCase": "off"
35+
},
36+
"style": {
37+
"noNonNullAssertion": "off",
38+
"useNodejsImportProtocol": "off",
39+
"useTemplate": "off",
40+
"useBlockStatements": "error",
41+
"noParameterAssign": "error",
42+
"useConst": "error",
43+
"useAsConstAssertion": "error",
44+
"useDefaultParameterLast": "error",
45+
"useEnumInitializers": "error",
46+
"useSelfClosingElements": "error",
47+
"useSingleVarDeclarator": "error",
48+
"noUnusedTemplateLiteral": "error",
49+
"useNumberNamespace": "error",
50+
"noInferrableTypes": "error",
51+
"noUselessElse": "error",
52+
"useImportType": "error"
53+
},
54+
"correctness": {
55+
"noUnusedImports": "error",
56+
"useExhaustiveDependencies": "off",
57+
"noUnknownFunction": "off",
58+
"noChildrenProp": "off",
59+
"noInnerDeclarations": "error"
60+
},
61+
"suspicious": {
62+
"noExplicitAny": "error",
63+
"noArrayIndexKey": "off",
64+
"noAssignInExpressions": "error",
65+
"noAsyncPromiseExecutor": "off",
66+
"noFallthroughSwitchClause": "error",
67+
"noConsole": "off",
68+
"noDoubleEquals": {
69+
"level": "error",
70+
"options": {
71+
"ignoreNull": false
72+
}
73+
},
74+
"noExtraNonNullAssertion": "error"
75+
},
76+
"performance": {
77+
"recommended": true,
78+
"noAccumulatingSpread": "error"
79+
},
80+
"security": {
81+
"recommended": true
82+
}
83+
}
84+
},
85+
"javascript": {
86+
"formatter": {
87+
"lineWidth": 100,
88+
"arrowParentheses": "always",
89+
"jsxQuoteStyle": "single",
90+
"attributePosition": "multiline",
91+
"quoteProperties": "asNeeded",
92+
"trailingCommas": "all",
93+
"semicolons": "always",
94+
"bracketSpacing": true,
95+
"bracketSameLine": false,
96+
"quoteStyle": "single"
97+
}
98+
}
99+
}

docs/prompt-caching.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Prompt Caching
2+
3+
OpenRouter maintains the canonical documentation for prompt caching at:
4+
5+
- **[OpenRouter Prompt Caching Guide](https://openrouter.ai/docs/features/prompt-caching)**
6+
7+
That guide covers provider-specific behavior, pricing, configuration requirements, and up-to-date usage examples. This repository intentionally links out to avoid duplicating content that can drift from production behavior.
8+
9+
## Examples
10+
11+
See ecosystem-specific examples in this repository for runnable reference implementations:
12+
13+
- **TypeScript + fetch**: [typescript/fetch/src/prompt-caching/](../typescript/fetch/src/prompt-caching/)

typescript/README.md

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# TypeScript Examples
2+
3+
A Bun monorepo containing OpenRouter examples across different TypeScript ecosystems.
4+
5+
## Structure
6+
7+
- **shared/** - Shared constants and utilities (LARGE_SYSTEM_PROMPT, types, etc.)
8+
- **fetch/** - Raw fetch API examples
9+
- **ai-sdk-v5/** - Vercel AI SDK v5 examples (using ai v4.x package)
10+
- **effect-ai/** - Effect-TS AI examples
11+
- **openrouter-sdk/** - OpenRouter TypeScript SDK examples (TODO)
12+
13+
## Prerequisites
14+
15+
- Bun runtime: `curl -fsSL https://bun.sh/install | bash`
16+
- `OPENROUTER_API_KEY` environment variable
17+
18+
## Installation
19+
20+
```bash
21+
# From repository root
22+
make install
23+
24+
# Or from the typescript/ directory
25+
cd typescript
26+
bun install
27+
```
28+
29+
## Running Examples
30+
31+
```bash
32+
# From repository root
33+
export OPENROUTER_API_KEY="your-key-here"
34+
make typescript
35+
36+
# Or from the typescript/ directory
37+
cd typescript
38+
bun examples
39+
40+
# Or run individual workspaces
41+
cd fetch && bun examples
42+
cd ai-sdk-v5 && bun examples
43+
cd effect-ai && bun examples
44+
```
45+
46+
## Workspace Benefits
47+
48+
1. **Shared constants** - LARGE_SYSTEM_PROMPT defined once in `shared/`
49+
2. **Consistent dependencies** - Managed at monorepo root with Bun workspaces
50+
3. **Type sharing** - Common types available across workspaces
51+
4. **Easy testing** - Run all examples from one location with `make typescript` or `bun examples`

typescript/fetch/README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
# TypeScript + fetch Examples
2+
3+
Raw HTTP examples using TypeScript and the native `fetch` API.
4+
5+
## Prerequisites
6+
7+
- Bun runtime: `curl -fsSL https://bun.sh/install | bash`
8+
- `OPENROUTER_API_KEY` environment variable
9+
10+
## Running Examples
11+
12+
```bash
13+
# From monorepo root (typescript/)
14+
bun examples
15+
16+
# Or from this workspace
17+
cd fetch
18+
bun examples
19+
```
20+
21+
## Features
22+
23+
- [prompt-caching](./src/prompt-caching/) - Anthropic caching examples
24+
25+
## Dependencies
26+
27+
- `@openrouter-examples/shared` - Shared constants (LARGE_SYSTEM_PROMPT) and types

typescript/fetch/package.json

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"name": "@openrouter-examples/fetch",
3+
"version": "1.0.0",
4+
"private": true,
5+
"type": "module",
6+
"scripts": {
7+
"examples": "bun run run-examples.ts",
8+
"typecheck": "tsc --noEmit"
9+
},
10+
"dependencies": {
11+
"@openrouter-examples/shared": "workspace:*"
12+
},
13+
"devDependencies": {
14+
"@types/bun": "1.3.2"
15+
}
16+
}

0 commit comments

Comments
 (0)