Skip to content

feat(cache): PatternDetector emits cache_control annotations#55

Merged
Siddhant-K-code merged 2 commits into
mainfrom
feat/53-pattern-detector-cache-control
May 2, 2026
Merged

feat(cache): PatternDetector emits cache_control annotations#55
Siddhant-K-code merged 2 commits into
mainfrom
feat/53-pattern-detector-cache-control

Conversation

@Siddhant-K-code

Copy link
Copy Markdown
Owner

Closes #53

What

Extends PatternDetector in pkg/cache to output a CacheAnnotation alongside each pattern classification, and adds AnnotateChunksForCache to produce a CacheControlPlan for a chunk slice.

Changes

pkg/cache/patterns.go

  • CacheAnnotation struct: Recommended, Reason, MinTokensMet, BoundaryAfter
  • DetectedPattern now carries TokenCount and CacheAnnotation
  • annotate(): pattern → annotation mapping
    • system_prompt, tool_definition, document → always recommended
    • code_block → conditional on token count ≥ 512
    • user messages → not recommended
  • AnnotateChunksForCache(chunks): walks chunks, selects up to 4 markers (Anthropic's limit) by highest token count, skips auto-placement when manual cache_control markers are already present in chunk metadata
  • CacheControlPlan / CacheMarker types

pkg/cache/cache_test.go

  • TestPatternDetector_CacheAnnotation: verifies annotation per pattern type
  • TestPatternDetector_AnnotateChunksForCache: 4-marker cap, manual marker passthrough, nil input

Why

PatternDetector already identifies cacheable content (system prompts, tool definitions, code blocks). This change connects that signal to Anthropic's cache_control field so callers get correct markers without needing to understand prompt caching mechanics. It also provides stability hints to the session-aware boundary manager (issue #51).

Extend PatternDetector to output a CacheAnnotation alongside each
pattern classification, and add AnnotateChunksForCache to produce a
CacheControlPlan for a chunk slice.

- CacheAnnotation: Recommended, Reason, MinTokensMet, BoundaryAfter
- Pattern → annotation mapping: system_prompt and tool_definition always
  recommended; code_block conditional on token count >= 512; document
  recommended; user messages not recommended
- AnnotateChunksForCache: selects up to 4 markers (Anthropic limit) by
  highest token count; skips auto-placement when manual cache_control
  markers are already present in chunk metadata
- DetectPattern now populates TokenCount on every DetectedPattern

Co-authored-by: Ona <[email protected]>
@Siddhant-K-code Siddhant-K-code merged commit 8569bb9 into main May 2, 2026
2 checks passed
@Siddhant-K-code Siddhant-K-code deleted the feat/53-pattern-detector-cache-control branch May 2, 2026 13:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Feature] PatternDetector → cache_control placement — use existing pattern signals to auto-annotate cacheable content

1 participant