-
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
base: main
Are you sure you want to change the base?
Conversation
This stack of pull requests is managed by Graphite. Learn more about stacking. |
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
|
Warning Review the following alerts detected in dependencies. According to your organization's Security Policy, it is recommended to resolve "Warn" alerts. Learn more about Socket for GitHub.
|
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.
Pull Request Overview
This PR adds comprehensive prompt caching examples for the fetch API, demonstrating Anthropic's prompt caching feature via OpenRouter. The PR includes documentation, example implementations, and build infrastructure with a Bun monorepo structure.
- Adds 3 prompt-caching example files demonstrating system message caching, user message caching, and multi-message conversation caching
- Creates shared workspace with reusable constants (LARGE_SYSTEM_PROMPT) and TypeScript types
- Adds comprehensive documentation in
docs/prompt-caching.mdcovering multiple providers - Sets up Bun monorepo with workspaces, Makefile orchestration, and Biome code quality tooling
Reviewed Changes
Copilot reviewed 17 out of 18 changed files in this pull request and generated 8 comments.
Show a summary per file
| File | Description |
|---|---|
| typescript/shared/tsconfig.json | TypeScript configuration for shared workspace with Bun types |
| typescript/shared/src/types.ts | Shared type definitions for chat completions, cache control, and usage metrics |
| typescript/shared/src/constants.ts | Large system prompt constant for cache testing and model identifiers |
| typescript/shared/package.json | Package configuration for shared workspace with exports |
| typescript/package.json | Root monorepo package with workspace definitions and scripts |
| typescript/fetch/tsconfig.json | TypeScript configuration for fetch examples workspace |
| typescript/fetch/src/prompt-caching/anthropic-user-message-cache.ts | Example demonstrating user message caching with cache_control |
| typescript/fetch/src/prompt-caching/anthropic-no-cache-control.ts | Control scenario example without cache_control markers |
| typescript/fetch/src/prompt-caching/anthropic-multi-message-cache.ts | Multi-turn conversation caching example |
| typescript/fetch/src/prompt-caching/README.md | Documentation for fetch API prompt caching examples |
| typescript/fetch/package.json | Fetch workspace package with example scripts |
| typescript/fetch/README.md | Overview of fetch examples workspace |
| typescript/README.md | TypeScript monorepo overview and documentation |
| docs/prompt-caching.md | Comprehensive prompt caching guide covering all providers |
| biome.jsonc | Code quality and formatting configuration |
| README.md | Repository root documentation and quick start guide |
| Makefile | Build orchestration for running examples |
| .gitignore | Git ignore patterns for dependencies and artifacts |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
typescript/fetch/src/prompt-caching/anthropic-user-message-cache.ts
Outdated
Show resolved
Hide resolved
typescript/fetch/src/prompt-caching/anthropic-no-cache-control.ts
Outdated
Show resolved
Hide resolved
typescript/fetch/src/prompt-caching/anthropic-multi-message-cache.ts
Outdated
Show resolved
Hide resolved
| @@ -0,0 +1,190 @@ | |||
| # Prompt Caching | |||
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
| "@biomejs/biome": "^1.9.4", | ||
| "typescript": "^5.7.2" |
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
| "@types/bun": "latest", | ||
| "typescript": "^5.7.2" |
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.
use fixed deps
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.
can we pul these types from the SDK instead (?)
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.
currently nothing we can just drop-in to do this; the types in the sdk have different casing for params and the ones with the correct casing aren't much better than adding as any;
planning on looking into this deeper in a separate PR
| // Use a large context document in the user message | ||
| const largeContext = `Here is a comprehensive TypeScript codebase to analyze:\n\n${LARGE_SYSTEM_PROMPT}`; | ||
|
|
||
| const requestBody = { |
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.
this shoudl satisfies something
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.
currently nothing we can just drop-in to do this; planning on looking into this deeper in a separate PR
| const examples = findExamples(srcDir); | ||
| console.log(`Found ${examples.length} example(s)\n`); | ||
|
|
||
| let failed = 0; |
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.
this bugs me a bit -- would prefer no let
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.
planning to refactor this script in a separate PR
louisgv
left a comment
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.
Woud ensure deps are frozen
Merge activity
|
The base branch was changed.
- 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
Replace hardcoded filename list with run-examples.ts that auto-discovers all .ts files in src/ Benefits: - Add new example → automatically included in 'bun examples' - Rename example → no package.json update needed - Impossible for package.json to reference non-existent files Also fixes stale filenames (user-message-cache.ts → anthropic-user-message-cache.ts)
af129be to
8be9df3
Compare
subtleGradient
left a comment
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.
will freeze deps and follow up with separate PRs for bringing stricter types to the fetch examples
| const examples = findExamples(srcDir); | ||
| console.log(`Found ${examples.length} example(s)\n`); | ||
|
|
||
| let failed = 0; |
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.
planning to refactor this script in a separate PR
| // Use a large context document in the user message | ||
| const largeContext = `Here is a comprehensive TypeScript codebase to analyze:\n\n${LARGE_SYSTEM_PROMPT}`; | ||
|
|
||
| const requestBody = { |
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.
currently nothing we can just drop-in to do this; planning on looking into this deeper in a separate PR
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.
currently nothing we can just drop-in to do this; the types in the sdk have different casing for params and the ones with the correct casing aren't much better than adding as any;
planning on looking into this deeper in a separate PR

Add prompt-caching examples for fetch API
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