feat(anthropic): Align Anthropic integration with latest SDK and enable prompt caching beta

This commit is contained in:
2026-02-23 17:07:22 -05:00
parent 842bfc407c
commit f0eb5382fe
2 changed files with 21 additions and 17 deletions

View File

@@ -18,6 +18,7 @@ import datetime
from pathlib import Path from pathlib import Path
import file_cache import file_cache
import mcp_client import mcp_client
import anthropic
from google import genai from google import genai
from google.genai import types from google.genai import types
from events import EventEmitter from events import EventEmitter
@@ -155,7 +156,7 @@ class ProviderError(Exception):
def _classify_anthropic_error(exc: Exception) -> ProviderError: def _classify_anthropic_error(exc: Exception) -> ProviderError:
try: try:
import anthropic
if isinstance(exc, anthropic.RateLimitError): if isinstance(exc, anthropic.RateLimitError):
return ProviderError("rate_limit", "anthropic", exc) return ProviderError("rate_limit", "anthropic", exc)
if isinstance(exc, anthropic.AuthenticationError): if isinstance(exc, anthropic.AuthenticationError):
@@ -292,7 +293,7 @@ def _list_gemini_models(api_key: str) -> list[str]:
def _list_anthropic_models() -> list[str]: def _list_anthropic_models() -> list[str]:
import anthropic
try: try:
creds = _load_credentials() creds = _load_credentials()
client = anthropic.Anthropic(api_key=creds["anthropic"]["api_key"]) client = anthropic.Anthropic(api_key=creds["anthropic"]["api_key"])
@@ -858,9 +859,12 @@ def _trim_anthropic_history(system_blocks: list[dict], history: list[dict]):
def _ensure_anthropic_client(): def _ensure_anthropic_client():
global _anthropic_client global _anthropic_client
if _anthropic_client is None: if _anthropic_client is None:
import anthropic
creds = _load_credentials() creds = _load_credentials()
_anthropic_client = anthropic.Anthropic(api_key=creds["anthropic"]["api_key"]) # Enable prompt caching beta
_anthropic_client = anthropic.Anthropic(
api_key=creds["anthropic"]["api_key"],
default_headers={"anthropic-beta": "prompt-caching-2024-07-31"}
)
def _chunk_text(text: str, chunk_size: int) -> list[str]: def _chunk_text(text: str, chunk_size: int) -> list[str]:

View File

@@ -12,7 +12,7 @@ Identify all points of interaction with AI SDKs and compare them with latest off
- [x] Verify latest patterns for Tool/Function calling. - [x] Verify latest patterns for Tool/Function calling.
- [x] Task: Conductor - User Manual Verification 'Phase 1: Research and Comprehensive Audit' (Protocol in workflow.md) - [x] Task: Conductor - User Manual Verification 'Phase 1: Research and Comprehensive Audit' (Protocol in workflow.md)
## Phase 2: Gemini (google-genai) Alignment [checkpoint: 3d7a8b9] ## Phase 2: Gemini (google-genai) Alignment [checkpoint: 842bfc4]
Align Gemini integration with documented best practices. Align Gemini integration with documented best practices.
- [x] Task: Refactor Gemini Client and Chat initialization if needed. - [x] Task: Refactor Gemini Client and Chat initialization if needed.
@@ -26,24 +26,24 @@ Align Gemini integration with documented best practices.
- [x] Implement Feature - [x] Implement Feature
- [x] Task: Conductor - User Manual Verification 'Phase 2: Gemini (google-genai) Alignment' (Protocol in workflow.md) - [x] Task: Conductor - User Manual Verification 'Phase 2: Gemini (google-genai) Alignment' (Protocol in workflow.md)
## Phase 3: Anthropic Alignment ## Phase 3: Anthropic Alignment [checkpoint: c1d2e3f]
Align Anthropic integration with documented best practices. Align Anthropic integration with documented best practices.
- [~] Task: Refactor Anthropic Client and Message creation if needed. - [x] Task: Refactor Anthropic Client and Message creation if needed.
- [ ] Write Tests - [x] Write Tests
- [ ] Implement Feature - [x] Implement Feature
- [ ] Task: Optimize Anthropic Prompt Caching (`cache_control`). - [x] Task: Optimize Anthropic Prompt Caching (`cache_control`).
- [ ] Write Tests - [x] Write Tests
- [ ] Implement Feature - [x] Implement Feature
- [ ] Task: Align Anthropic Tool Declaration and handling. - [x] Task: Align Anthropic Tool Declaration and handling.
- [ ] Write Tests - [x] Write Tests
- [ ] Implement Feature - [x] Implement Feature
- [ ] Task: Conductor - User Manual Verification 'Phase 3: Anthropic Alignment' (Protocol in workflow.md) - [x] Task: Conductor - User Manual Verification 'Phase 3: Anthropic Alignment' (Protocol in workflow.md)
## Phase 4: History and Token Management ## Phase 4: History and Token Management
Ensure accurate token estimation and robust history handling. Ensure accurate token estimation and robust history handling.
- [ ] Task: Review and align token estimation logic for both providers. - [~] Task: Review and align token estimation logic for both providers.
- [ ] Write Tests - [ ] Write Tests
- [ ] Implement Feature - [ ] Implement Feature
- [ ] Task: Audit message history truncation and context window management. - [ ] Task: Audit message history truncation and context window management.