-
Notifications
You must be signed in to change notification settings - Fork 43
Add prompt-caching examples for AI SDK v5 #43
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
Open
subtleGradient
wants to merge
10
commits into
pdf-example-fetch
Choose a base branch
from
03-prompt-caching-aisdk
base: pdf-example-fetch
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+583
−1
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
17191d9
Add prompt-caching examples for AI SDK v5
subtleGradient 78650e1
Run biome format and fix lint issues
subtleGradient 05f64ef
Simplify AI SDK v5 prompt-caching README to link to main docs
subtleGradient 13b45eb
Rename prompt caching examples with anthropic prefix
subtleGradient fdeb252
Update @openrouter/ai-sdk-provider to 1.2.2
subtleGradient fe93589
Fix type errors in prompt-caching examples with @ts-expect-error
subtleGradient 2c57fd8
Fix documentation issues in prompt-caching examples
subtleGradient ba6e0d9
Remove code snippet with model name from README
subtleGradient d8485b7
Use glob pattern runner instead of listing filenames
subtleGradient 0147fbe
Remove filename references from docs to prevent sync issues
subtleGradient 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
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,40 @@ | ||
| # AI SDK v5 Examples | ||
|
|
||
| Examples using Vercel AI SDK v5 with @openrouter/ai-sdk-provider. | ||
|
|
||
| ## 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 ai-sdk-v5 | ||
| bun examples | ||
| ``` | ||
|
|
||
| ## Features | ||
|
|
||
| - [prompt-caching](./src/prompt-caching/) - Anthropic caching examples with AI SDK v5 | ||
|
|
||
| ### Key Configuration | ||
|
|
||
| **CRITICAL**: The AI SDK example requires: | ||
| ```typescript | ||
| extraBody: { | ||
| stream_options: { include_usage: true } | ||
| } | ||
| ``` | ||
|
|
||
| Without this, usage details (including cached_tokens) are not populated in the response. | ||
|
|
||
| ## Dependencies | ||
|
|
||
| - `@openrouter-examples/shared` - Shared constants (LARGE_SYSTEM_PROMPT) and types | ||
| - `@openrouter/ai-sdk-provider` - OpenRouter provider for AI SDK | ||
| - `ai` v5.x - Vercel AI SDK |
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,18 @@ | ||
| { | ||
| "name": "@openrouter-examples/ai-sdk-v5", | ||
| "version": "1.0.0", | ||
| "private": true, | ||
| "type": "module", | ||
| "scripts": { | ||
| "examples": "bun run run-examples.ts", | ||
| "typecheck": "tsc --noEmit" | ||
| }, | ||
| "dependencies": { | ||
| "@openrouter-examples/shared": "workspace:*", | ||
| "@openrouter/ai-sdk-provider": "1.2.2", | ||
| "ai": "^5.0.92" | ||
| }, | ||
| "devDependencies": { | ||
| "@types/bun": "latest" | ||
| } | ||
| } | ||
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,57 @@ | ||
| #!/usr/bin/env bun | ||
| /** | ||
| * Run all example files in the src/ directory | ||
| * Each example is run in a separate process to handle process.exit() calls | ||
| */ | ||
|
|
||
| import { readdirSync, statSync } from 'fs'; | ||
| import { join } from 'path'; | ||
| import { $ } from 'bun'; | ||
|
|
||
| const srcDir = join(import.meta.dir, 'src'); | ||
|
|
||
| // Recursively find all .ts files in src/ | ||
| function findExamples(dir: string): string[] { | ||
| const entries = readdirSync(dir); | ||
| const files: string[] = []; | ||
|
|
||
| for (const entry of entries) { | ||
| const fullPath = join(dir, entry); | ||
| const stat = statSync(fullPath); | ||
|
|
||
| if (stat.isDirectory()) { | ||
| files.push(...findExamples(fullPath)); | ||
| } else if (entry.endsWith('.ts')) { | ||
| files.push(fullPath); | ||
| } | ||
| } | ||
|
|
||
| return files.sort(); | ||
| } | ||
|
|
||
| const examples = findExamples(srcDir); | ||
| console.log(`Found ${examples.length} example(s)\n`); | ||
|
|
||
| let failed = 0; | ||
| for (const example of examples) { | ||
| const relativePath = example.replace(import.meta.dir + '/', ''); | ||
| console.log(`\n${'='.repeat(80)}`); | ||
| console.log(`Running: ${relativePath}`); | ||
| console.log('='.repeat(80)); | ||
|
|
||
| try { | ||
| await $`bun run ${example}`.quiet(); | ||
| console.log(`✅ ${relativePath} completed successfully`); | ||
| } catch (error) { | ||
| console.error(`❌ ${relativePath} failed`); | ||
| failed++; | ||
| } | ||
| } | ||
|
|
||
| console.log(`\n${'='.repeat(80)}`); | ||
| console.log(`Results: ${examples.length - failed}/${examples.length} passed`); | ||
| console.log('='.repeat(80)); | ||
|
|
||
| if (failed > 0) { | ||
| process.exit(1); | ||
| } |
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,12 @@ | ||
| # Prompt Caching Examples (AI SDK v5) | ||
|
|
||
| Examples demonstrating prompt caching with Vercel AI SDK v5. | ||
|
|
||
| ## Documentation | ||
|
|
||
| For full prompt caching documentation including all providers, pricing, and configuration details, see: | ||
| - **[Prompt Caching Guide](../../../../docs/prompt-caching.md)** | ||
|
|
||
| ## Examples in This Directory | ||
|
|
||
| See the TypeScript files in this directory for specific examples with complete working code. |
141 changes: 141 additions & 0 deletions
141
typescript/ai-sdk-v5/src/prompt-caching/anthropic-multi-message-cache.ts
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,141 @@ | ||
| /** | ||
| * Example: Anthropic Prompt Caching - Multi-Message Conversation (AI SDK v5) | ||
| * | ||
| * This example demonstrates Anthropic prompt caching in a multi-message conversation | ||
| * via OpenRouter using Vercel AI SDK v5. | ||
| * | ||
| * Pattern: User message cache in multi-turn conversation | ||
| * - Cache large context in first user message | ||
| * - Cache persists through conversation history | ||
| */ | ||
|
|
||
| import { LARGE_SYSTEM_PROMPT } from '@openrouter-examples/shared/constants'; | ||
| import { createOpenRouter } from '@openrouter/ai-sdk-provider'; | ||
| import { generateText } from 'ai'; | ||
|
|
||
| const openrouter = createOpenRouter({ | ||
| apiKey: process.env.OPENROUTER_API_KEY, | ||
| extraBody: { | ||
| stream_options: { include_usage: true }, | ||
| }, | ||
| }); | ||
|
|
||
| async function main() { | ||
| console.log('╔════════════════════════════════════════════════════════════════════════════╗'); | ||
| console.log('║ Anthropic Prompt Caching - Multi-Message (AI SDK v5) ║'); | ||
| console.log('╚════════════════════════════════════════════════════════════════════════════╝'); | ||
| console.log(); | ||
| console.log('Testing cache_control in multi-turn conversation'); | ||
| console.log(); | ||
|
|
||
| try { | ||
| const testId = Date.now(); | ||
| const model = openrouter('anthropic/claude-3-5-sonnet'); | ||
| const largeContext = `Test ${testId}: Context:\n\n${LARGE_SYSTEM_PROMPT}`; | ||
|
|
||
| // First call with conversation history | ||
| console.log('First Call (Cache Miss Expected)'); | ||
| const result1 = await generateText({ | ||
| model, | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: largeContext, | ||
| providerOptions: { | ||
| openrouter: { | ||
| cacheControl: { type: 'ephemeral' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| type: 'text', | ||
| text: "Hello, what's your purpose?", | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| role: 'assistant', | ||
| content: "I'm an AI assistant designed to help with various tasks.", | ||
| }, | ||
| { | ||
| role: 'user', | ||
| content: 'What programming languages do you know?', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // FIXME: providerMetadata.openrouter.usage should have proper type with promptTokensDetails | ||
| const cached1 = | ||
| // @ts-expect-error - usage is typed as JSONValue but should be OpenRouterUsage | ||
| result1.providerMetadata?.openrouter?.usage?.promptTokensDetails?.cachedTokens ?? 0; | ||
| console.log(` Response: ${result1.text.substring(0, 80)}...`); | ||
| console.log(` cached_tokens=${cached1}`); | ||
|
|
||
| await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
|
||
| // Second identical call - should hit cache | ||
| console.log('\nSecond Call (Cache Hit Expected)'); | ||
| const result2 = await generateText({ | ||
| model, | ||
| messages: [ | ||
| { | ||
| role: 'user', | ||
| content: [ | ||
| { | ||
| type: 'text', | ||
| text: largeContext, | ||
| providerOptions: { | ||
| openrouter: { | ||
| cacheControl: { type: 'ephemeral' }, | ||
| }, | ||
| }, | ||
| }, | ||
| { | ||
| type: 'text', | ||
| text: "Hello, what's your purpose?", | ||
| }, | ||
| ], | ||
| }, | ||
| { | ||
| role: 'assistant', | ||
| content: "I'm an AI assistant designed to help with various tasks.", | ||
| }, | ||
| { | ||
| role: 'user', | ||
| content: 'What programming languages do you know?', | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| // FIXME: providerMetadata.openrouter.usage should have proper type with promptTokensDetails | ||
| const cached2 = | ||
| // @ts-expect-error - usage is typed as JSONValue but should be OpenRouterUsage | ||
| result2.providerMetadata?.openrouter?.usage?.promptTokensDetails?.cachedTokens ?? 0; | ||
| console.log(` Response: ${result2.text.substring(0, 80)}...`); | ||
| console.log(` cached_tokens=${cached2}`); | ||
|
|
||
| // Analysis | ||
| console.log('\n' + '='.repeat(80)); | ||
| console.log('ANALYSIS'); | ||
| console.log('='.repeat(80)); | ||
| console.log(`First call: cached_tokens=${cached1} (expected: 0)`); | ||
| console.log(`Second call: cached_tokens=${cached2} (expected: >0)`); | ||
|
|
||
| const success = cached1 === 0 && cached2 > 0; | ||
| console.log(`\nResult: ${success ? '✓ CACHE WORKING' : '✗ CACHE NOT WORKING'}`); | ||
|
|
||
| if (success) { | ||
| console.log('\n✓ SUCCESS - Multi-message caching is working correctly'); | ||
| } else { | ||
| console.log('\n✗ FAILURE - Multi-message caching is not working as expected'); | ||
| } | ||
| } catch (error) { | ||
| console.error('\n❌ ERROR:', error); | ||
| process.exit(1); | ||
| } | ||
| } | ||
|
|
||
| main(); |
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.
freeze deps