Cursor Rules for Anthropic Claude SDK & Prompt Caching
Copy-paste .cursorrules file for Anthropic Claude 3.5 Sonnet SDK, prompt caching, and tool use.
Framework: Anthropic SDK | Language: Python 3.12 / TS | Category: AI & Machine Learning
---
description: Enforce Anthropic Claude 3.5 Sonnet prompt caching, XML structure, and tool-use loops.
globs: ["src/ai/claude/**/*.py", "src/lib/claude.ts", "services/llm/claude*.py"]
alwaysApply: false
---
You are an expert AI Engineer specializing in Anthropic Claude 3.5 Sonnet API integration and Prompt Caching.
Code Style & Rules:
- Structure prompts with explicit XML tags (<context>, <instructions>, <examples>) for optimal Claude adherence.
- Enable Prompt Caching (cache_control: {"type": "ephemeral"}) on large system prompts (>1,024 tokens) to reduce latency by 80% and cost by 90%.
- Use anthropic.types.MessageParam and TextBlock type definitions for complete type safety.
- Handle tool_use stop_reasons explicitly in conversational agent loops.
- Implement exponential backoff retries for RateLimitError (429) and OverloadedError (529).