Skip to content

Conversation

@subtleGradient
Copy link
Contributor

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

Copy link
Contributor Author

subtleGradient commented Nov 11, 2025

@socket-security
Copy link

socket-security bot commented Nov 11, 2025

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Added@​types/​bun@​1.3.21001004995100
Addedtypescript@​5.9.31001009010090
Added@​biomejs/​biome@​1.9.49310010099100

View full report

@socket-security
Copy link

socket-security bot commented Nov 11, 2025

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.

Action Severity Alert  (click "▶" to expand/collapse)
Warn High
License policy violation: npm typescript under MIT-Khronos-old

License: MIT-Khronos-old (package/ThirdPartyNoticeText.txt)

License: CC-BY-4.0 (package/ThirdPartyNoticeText.txt)

License: LicenseRef-W3C-Community-Final-Specification-Agreement (package/ThirdPartyNoticeText.txt)

From: typescript/package.jsonnpm/typescript@5.9.3

ℹ Read more on: This package | This alert | What is a license policy violation?

Next steps: Take a moment to review the security alert above. Review the linked package source code to understand the potential risk. Ensure the package is not malicious before proceeding. If you're unsure how to proceed, reach out to your security team or ask the Socket team for help at support@socket.dev.

Suggestion: Find a package that does not violate your license policy or adjust your policy to allow this package's license.

Mark the package as acceptable risk. To ignore this alert only in this pull request, reply with the comment @SocketSecurity ignore npm/typescript@5.9.3. You can also ignore all packages with @SocketSecurity ignore-all. To ignore an alert for all future pull requests, use Socket's Dashboard to change the triage state of this alert.

View full report

@subtleGradient subtleGradient marked this pull request as ready for review November 11, 2025 23:53
Copilot AI review requested due to automatic review settings November 11, 2025 23:53
Copilot finished reviewing on behalf of subtleGradient November 11, 2025 23:54
Copy link

Copilot AI left a 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.md covering 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.

@@ -0,0 +1,190 @@
# Prompt Caching
Copy link
Contributor Author

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

Comment on lines +18 to +19
"@biomejs/biome": "^1.9.4",
"typescript": "^5.7.2"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

freeze deps

Comment on lines +14 to +15
"@types/bun": "latest",
"typescript": "^5.7.2"
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use fixed deps

Copy link

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 (?)

Copy link
Contributor Author

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 = {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this shoudl satisfies something

Copy link
Contributor Author

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;
Copy link

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

Copy link
Contributor Author

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
louisgv previously approved these changes Nov 13, 2025
Copy link

@louisgv louisgv left a 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

Copy link
Contributor Author

subtleGradient commented Nov 13, 2025

Merge activity

  • Nov 13, 2:47 PM UTC: A user started a stack merge that includes this pull request via Graphite.
  • Nov 13, 2:48 PM UTC: Graphite rebased this pull request as part of a merge.
  • Nov 13, 2:50 PM UTC: Graphite couldn't merge this PR because it was not satisfying all requirements (PR does not have required approvals).

@subtleGradient subtleGradient changed the base branch from 01-clean-slate to graphite-base/42 November 13, 2025 14:48
@subtleGradient subtleGradient changed the base branch from graphite-base/42 to main November 13, 2025 14:48
@subtleGradient subtleGradient dismissed louisgv’s stale review November 13, 2025 14:48

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)
@subtleGradient subtleGradient force-pushed the 02-prompt-caching-fetch branch from af129be to 8be9df3 Compare November 13, 2025 14:48
Copy link
Contributor Author

@subtleGradient subtleGradient left a 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;
Copy link
Contributor Author

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 = {
Copy link
Contributor Author

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

Copy link
Contributor Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants