-
Notifications
You must be signed in to change notification settings - Fork 43
Add prompt-caching examples for fetch API #42
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| node_modules/ | ||
| typescript/node_modules/ | ||
| typescript/*/node_modules/ | ||
| typescript/bun.lock | ||
| *.log | ||
| .DS_Store |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| # Makefile - Root orchestration for openrouter-examples | ||
|
|
||
| .PHONY: help examples typescript install clean | ||
|
|
||
| help: | ||
| @echo "OpenRouter Examples - Available commands:" | ||
| @echo "" | ||
| @echo " make examples - Run all examples" | ||
| @echo " make typescript - Run TypeScript monorepo examples" | ||
| @echo " make install - Install TypeScript dependencies" | ||
| @echo " make clean - Clean node_modules and lockfiles" | ||
| @echo "" | ||
|
|
||
| # Run all examples | ||
| examples: typescript | ||
|
|
||
| # Run TypeScript monorepo examples | ||
| typescript: | ||
| @echo "=== Running TypeScript examples ===" | ||
| @cd typescript && bun examples | ||
|
|
||
| # Install TypeScript dependencies | ||
| install: | ||
| @echo "=== Installing TypeScript dependencies ===" | ||
| @cd typescript && bun install | ||
|
|
||
| # Clean build artifacts | ||
| clean: | ||
| @echo "=== Cleaning TypeScript artifacts ===" | ||
| @rm -rf typescript/node_modules | ||
| @rm -rf typescript/*/node_modules | ||
| @rm -rf typescript/bun.lock | ||
| @echo "Clean complete" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| # OpenRouter Examples | ||
|
|
||
| Comprehensive, tested, executable examples demonstrating OpenRouter features across multiple ecosystems. | ||
|
|
||
| ## Quick Start | ||
|
|
||
| ```bash | ||
| # Set your API key | ||
| export OPENROUTER_API_KEY="your-key-here" | ||
|
|
||
| # Run all examples | ||
| make examples | ||
|
|
||
| # Or run specific ecosystems | ||
| make curl # Run curl examples | ||
| make typescript # Run TypeScript monorepo examples | ||
| ``` | ||
|
|
||
| ## Repository Structure | ||
|
|
||
| ``` | ||
| . | ||
| ├── curl/ - Shell script examples | ||
| ├── typescript/ - TypeScript monorepo (Bun workspaces) | ||
| │ ├── shared/ - Shared constants and types | ||
| │ ├── fetch/ - Raw fetch API examples | ||
| │ ├── ai-sdk-v5/ - Vercel AI SDK v5 examples | ||
| │ ├── effect-ai/ - Effect-TS examples | ||
| │ └── openrouter-sdk/ - OpenRouter SDK examples (TODO) | ||
| ├── docs/ - Feature documentation | ||
| └── Makefile - Unified command interface | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| ### Prompt Caching | ||
| - **Documentation**: [docs/prompt-caching.md](docs/prompt-caching.md) | ||
| - **Examples**: | ||
| - [curl/prompt-caching.sh](curl/prompt-caching.sh) | ||
| - [typescript/fetch/src/prompt-caching/](typescript/fetch/src/prompt-caching/) | ||
| - [typescript/ai-sdk-v5/src/prompt-caching/](typescript/ai-sdk-v5/src/prompt-caching/) | ||
| - [typescript/effect-ai/src/prompt-caching/](typescript/effect-ai/src/prompt-caching/) | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Bun runtime: `curl -fsSL https://bun.sh/install | bash` | ||
| - OpenRouter API key: [https://openrouter.ai/keys](https://openrouter.ai/keys) | ||
| - For curl examples: `jq` (JSON processor) | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| # Install TypeScript dependencies | ||
| make install | ||
|
|
||
| # Or manually | ||
| cd typescript && bun install | ||
| ``` | ||
|
|
||
| ## Running Examples | ||
|
|
||
| ### All Examples | ||
| ```bash | ||
| make examples | ||
| ``` | ||
|
|
||
| ### By Ecosystem | ||
| ```bash | ||
| make curl # Shell scripts with curl + jq | ||
| make typescript # All TypeScript examples (fetch, AI SDK, Effect) | ||
| ``` | ||
|
|
||
| ### Individual Examples | ||
| ```bash | ||
| # curl | ||
| bash curl/prompt-caching.sh | ||
|
|
||
| # TypeScript | ||
| cd typescript/fetch && bun examples | ||
| cd typescript/ai-sdk-v5 && bun examples | ||
| cd typescript/effect-ai && bun examples | ||
| ``` | ||
|
|
||
| ## Benefits | ||
|
|
||
| ### For Users | ||
| 1. **Copy-paste ready** - All examples are runnable as-is | ||
| 2. **Tested and proven** - Every example has been verified to work | ||
| 3. **Evidence-based** - Examples show expected outputs and verification | ||
| 4. **Multiple ecosystems** - Choose the one that matches your stack | ||
|
|
||
| ### For Developers | ||
| 1. **Single source of truth** - Constants defined once in `typescript/shared/` | ||
| 2. **Type safety** - Shared types across all TypeScript examples | ||
| 3. **Consistent patterns** - Each ecosystem follows its own idioms | ||
| 4. **Easy maintenance** - Bun monorepo for TypeScript workspaces | ||
|
|
||
| ## Contributing | ||
|
|
||
| See individual ecosystem READMEs: | ||
| - [curl/README.md](curl/README.md) | ||
| - [typescript/README.md](typescript/README.md) | ||
|
|
||
| ## License | ||
|
|
||
| See [LICENSE.md](LICENSE.md) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| { | ||
| "$schema": "https://biomejs.dev/schemas/2.3.3/schema.json", | ||
| "vcs": { | ||
| "enabled": true, | ||
| "clientKind": "git", | ||
| "useIgnoreFile": true | ||
| }, | ||
| "files": { | ||
| "ignoreUnknown": false, | ||
| "ignore": [ | ||
| "**/*.json", | ||
| "!biome.json" | ||
| ] | ||
| }, | ||
| "formatter": { | ||
| "enabled": true, | ||
| "indentStyle": "space", | ||
| "indentWidth": 2, | ||
| "lineWidth": 100, | ||
| "attributePosition": "multiline" | ||
| }, | ||
| "organizeImports": { | ||
| "enabled": true | ||
| }, | ||
| "linter": { | ||
| "enabled": true, | ||
| "rules": { | ||
| "recommended": true, | ||
| "complexity": { | ||
| "useLiteralKeys": "off", | ||
| "noExtraBooleanCast": "off", | ||
| "noForEach": "off", | ||
| "noBannedTypes": "error", | ||
| "noUselessSwitchCase": "off" | ||
| }, | ||
| "style": { | ||
| "noNonNullAssertion": "off", | ||
| "useNodejsImportProtocol": "off", | ||
| "useTemplate": "off", | ||
| "useBlockStatements": "error", | ||
| "noParameterAssign": "error", | ||
| "useConst": "error", | ||
| "useAsConstAssertion": "error", | ||
| "useDefaultParameterLast": "error", | ||
| "useEnumInitializers": "error", | ||
| "useSelfClosingElements": "error", | ||
| "useSingleVarDeclarator": "error", | ||
| "noUnusedTemplateLiteral": "error", | ||
| "useNumberNamespace": "error", | ||
| "noInferrableTypes": "error", | ||
| "noUselessElse": "error", | ||
| "useImportType": "error" | ||
| }, | ||
| "correctness": { | ||
| "noUnusedImports": "error", | ||
| "useExhaustiveDependencies": "off", | ||
| "noUnknownFunction": "off", | ||
| "noChildrenProp": "off", | ||
| "noInnerDeclarations": "error" | ||
| }, | ||
| "suspicious": { | ||
| "noExplicitAny": "error", | ||
| "noArrayIndexKey": "off", | ||
| "noAssignInExpressions": "error", | ||
| "noAsyncPromiseExecutor": "off", | ||
| "noFallthroughSwitchClause": "error", | ||
| "noConsole": "off", | ||
| "noDoubleEquals": { | ||
| "level": "error", | ||
| "options": { | ||
| "ignoreNull": false | ||
| } | ||
| }, | ||
| "noExtraNonNullAssertion": "error" | ||
| }, | ||
| "performance": { | ||
| "recommended": true, | ||
| "noAccumulatingSpread": "error" | ||
| }, | ||
| "security": { | ||
| "recommended": true | ||
| } | ||
| } | ||
| }, | ||
| "javascript": { | ||
| "formatter": { | ||
| "lineWidth": 100, | ||
| "arrowParentheses": "always", | ||
| "jsxQuoteStyle": "single", | ||
| "attributePosition": "multiline", | ||
| "quoteProperties": "asNeeded", | ||
| "trailingCommas": "all", | ||
| "semicolons": "always", | ||
| "bracketSpacing": true, | ||
| "bracketSameLine": false, | ||
| "quoteStyle": "single" | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| # Prompt Caching | ||
|
|
||
| OpenRouter maintains the canonical documentation for prompt caching at: | ||
|
|
||
| - **[OpenRouter Prompt Caching Guide](https://openrouter.ai/docs/features/prompt-caching)** | ||
|
|
||
| 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. | ||
|
|
||
| ## Examples | ||
|
|
||
| See ecosystem-specific examples in this repository for runnable reference implementations: | ||
|
|
||
| - **TypeScript + fetch**: [typescript/fetch/src/prompt-caching/](../typescript/fetch/src/prompt-caching/) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| # TypeScript Examples | ||
|
|
||
| A Bun monorepo containing OpenRouter examples across different TypeScript ecosystems. | ||
|
|
||
| ## Structure | ||
|
|
||
| - **shared/** - Shared constants and utilities (LARGE_SYSTEM_PROMPT, types, etc.) | ||
| - **fetch/** - Raw fetch API examples | ||
| - **ai-sdk-v5/** - Vercel AI SDK v5 examples (using ai v4.x package) | ||
| - **effect-ai/** - Effect-TS AI examples | ||
| - **openrouter-sdk/** - OpenRouter TypeScript SDK examples (TODO) | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Bun runtime: `curl -fsSL https://bun.sh/install | bash` | ||
| - `OPENROUTER_API_KEY` environment variable | ||
|
|
||
| ## Installation | ||
|
|
||
| ```bash | ||
| # From repository root | ||
| make install | ||
|
|
||
| # Or from the typescript/ directory | ||
| cd typescript | ||
| bun install | ||
| ``` | ||
|
|
||
| ## Running Examples | ||
|
|
||
| ```bash | ||
| # From repository root | ||
| export OPENROUTER_API_KEY="your-key-here" | ||
| make typescript | ||
|
|
||
| # Or from the typescript/ directory | ||
| cd typescript | ||
| bun examples | ||
|
|
||
| # Or run individual workspaces | ||
| cd fetch && bun examples | ||
| cd ai-sdk-v5 && bun examples | ||
| cd effect-ai && bun examples | ||
| ``` | ||
|
|
||
| ## Workspace Benefits | ||
|
|
||
| 1. **Shared constants** - LARGE_SYSTEM_PROMPT defined once in `shared/` | ||
| 2. **Consistent dependencies** - Managed at monorepo root with Bun workspaces | ||
| 3. **Type sharing** - Common types available across workspaces | ||
| 4. **Easy testing** - Run all examples from one location with `make typescript` or `bun examples` |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| # TypeScript + fetch Examples | ||
|
|
||
| Raw HTTP examples using TypeScript and the native `fetch` API. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - Bun runtime: `curl -fsSL https://bun.sh/install | bash` | ||
| - `OPENROUTER_API_KEY` environment variable | ||
|
|
||
| ## Running Examples | ||
|
|
||
| ```bash | ||
| # From monorepo root (typescript/) | ||
| bun examples | ||
|
|
||
| # Or from this workspace | ||
| cd fetch | ||
| bun examples | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| - [prompt-caching](./src/prompt-caching/) - Anthropic caching examples | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - `@openrouter-examples/shared` - Shared constants (LARGE_SYSTEM_PROMPT) and types |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| { | ||
| "name": "@openrouter-examples/fetch", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "examples": "bun run run-examples.ts", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "dependencies": { | ||
| "@openrouter-examples/shared": "workspace:*" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/bun": "1.3.2" | ||
| } | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: link to https://openrouter.ai/docs/features/prompt-caching instead of duplicating the content here