Skip to content

Commit a4f9bfe

Browse files
Merge pull request #164 from Annotation-Garden/feature/phase2-universal-caching-observability
Complete Phase 2: prompt-cache measurement, usage reporting, and migration cleanup
2 parents 1d6ff90 + 9ecbb62 commit a4f9bfe

39 files changed

Lines changed: 2470 additions & 328 deletions

.context/agent-architecture.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ HEDit uses a LangGraph-based multi-agent workflow to convert natural language ev
6868

6969
### 6. VisionAgent (`src/agents/vision_agent.py`)
7070
- **Input**: Base64-encoded image
71-
- **Process**: Uses vision-language models (e.g., Qwen-VL via OpenRouter)
71+
- **Process**: Uses a Claude model (natively multimodal; no separate vision vendor)
7272
- **Output**: Natural language description of image content
7373
- **Key feature**: Enables image-to-HED annotation pipeline
7474

@@ -100,7 +100,12 @@ Defined in `src/agents/state.py` as `HedAnnotationState(TypedDict)`:
100100

101101
## Configuration
102102

103-
- **LLM Provider**: OpenRouter API (production), with Ollama fallback
103+
- **LLM Provider**: Anthropic Claude via the Claude Platform on AWS (Anthropic-operated
104+
Messages API, AWS Marketplace billing; not Amazon Bedrock)
105+
- **Offered models**: `claude-haiku-4-5` (default) and `claude-sonnet-5`
104106
- **Default model**: Configurable via environment/headers
105-
- **BYOK support**: Users can provide their own API keys
107+
- **BYOK support**: Users can provide their own Anthropic key (`X-Anthropic-Key`),
108+
which routes to api.anthropic.com
109+
- **Prompt caching**: The annotation system prompt is cached; usage and savings are
110+
reported per request (see `docs/prompt-caching.md`)
106111
- **Streaming**: LangGraph `astream_events` for real-time progress via SSE

.context/api-and-deployment.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,17 +11,23 @@
1111
- `POST /feedback`: User feedback submission
1212
- `GET /health`: Service health check
1313
- `GET /version`: Version information
14+
- `GET /metrics`: Token use, cost, and prompt-cache savings since startup
15+
(server API key required; BYOK callers get 403)
1416

1517
### Authentication Modes
16-
1. **Server mode**: `X-API-Key` header (server's OpenRouter key)
17-
2. **BYOK mode**: `X-OpenRouter-Key` header (user's own API key)
18+
1. **Server mode**: `X-API-Key` header (server's Anthropic credentials are used)
19+
2. **BYOK mode**: `X-Anthropic-Key` header (user's own Anthropic key, `sk-ant-...`,
20+
routed to api.anthropic.com)
1821
3. **Public endpoints**: `/feedback`, `/health`, `/version`
1922

2023
### Model Override Headers
21-
- `X-OpenRouter-Model`: Override annotation model
22-
- `X-OpenRouter-Vision-Model`: Override vision model
23-
- `X-OpenRouter-Provider`: Override provider routing
24-
- `X-OpenRouter-Temperature`: Override temperature
24+
- `X-Anthropic-Model`: Override annotation model
25+
- `X-Anthropic-Eval-Model`: Override evaluation model
26+
- `X-Anthropic-Vision-Model`: Override vision model
27+
- `X-Anthropic-Temperature`: Override temperature
28+
29+
The legacy `X-OpenRouter-*` spellings of these names (plus `X-OpenRouter-Key`) are
30+
still accepted as transport; provider-routing headers are ignored.
2531

2632
### CORS
2733
- Production: `hedit.pages.dev`, `annotation.garden`

.env.example

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,18 @@ VISION_MODEL=claude-haiku-4-5
7474
# For highest annotation quality (slower, more expensive):
7575
# ANNOTATION_MODEL=claude-sonnet-5
7676

77+
# ============================================================================
78+
# Prompt Cache Lifetime
79+
# ============================================================================
80+
# The annotation system prompt (~21.8k tokens of HED vocabulary guide) is
81+
# cached, which cuts input cost by ~90% on a cache hit. Entries live 5
82+
# minutes by default, which suits server traffic; a cache write costs 1.25x,
83+
# so break-even is the second request inside the window.
84+
# Set to 1h for interactive use where requests are minutes apart (a 1-hour
85+
# write costs 2x and pays off from the third request in the hour).
86+
# See docs/prompt-caching.md
87+
# HEDIT_PROMPT_CACHE_TTL=5m
88+
7789
# ============================================================================
7890
# HED Configuration
7991
# ============================================================================

.rules/testing.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@ Never use mocks, stubs, or fake datasets. If real testing is not possible, don't
1414

1515
## When to Write Tests
1616
- **DO:** Test with real data and actual dependencies
17-
- **DO:** Use test API keys (`OPENROUTER_API_KEY_FOR_TESTING`)
17+
- **DO:** Use the Anthropic test credentials (`ANTHROPIC_API_KEY`, with
18+
`ANTHROPIC_BASE_URL` and `ANTHROPIC_WORKSPACE_ID` for the Claude Platform on AWS)
1819
- **DO:** Test against actual file systems and real HED schemas
1920
- **DON'T:** Write tests if only mocks would work
2021
- **DON'T:** Create artificial test scenarios

.serena/memories/codebase_structure.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,8 @@ hedit/
2222
│ │ ├── json_schema_loader.py # JSON schema + vocabulary extraction
2323
│ │ ├── schema_loader.py # Legacy Python schema loader
2424
│ │ ├── error_remediation.py # Error augmentation for LLM feedback
25-
│ │ ├── openrouter_llm.py # OpenRouter API integration
25+
│ │ ├── anthropic_llm.py # Anthropic Claude integration (prompt caching)
26+
│ │ ├── llm_usage.py # Token, cost, and cache accounting
2627
│ │ ├── litellm_llm.py # Alternative LLM providers
2728
│ │ ├── image_processing.py # Base64 image encoding
2829
│ │ └── github_client.py # GitHub API for feedback issues

.serena/memories/project_overview.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ HEDit is a multi-agent system for converting natural language event descriptions
77
- **Language**: Python 3.12+
88
- **Package Manager**: uv
99
- **Agent Framework**: LangGraph
10-
- **LLM Provider**: OpenRouter API (production), Ollama (fallback)
10+
- **LLM Provider**: Anthropic Claude via the Claude Platform on AWS (claude-haiku-4-5 default, claude-sonnet-5 optional)
1111
- **Validation**: HED JavaScript validator + HED Python tools (hedtools)
1212
- **Backend**: FastAPI
1313
- **CLI**: Typer + Rich

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ Config files are stored in `~/.config/hedit/`:
6767
- **Image Annotation**: Annotate visual stimuli directly from image files
6868
- **Multi-Stage Validation**: AI agents generate, validate, evaluate, and refine annotations
6969
- **Claude-Powered**: Anthropic Claude models (Haiku 4.5 default, Sonnet 5 optional); bring your own Anthropic key if you prefer your own billing
70+
- **Cost Transparency**: Every annotation reports its token use and how much prompt caching saved (typically ~80% of input cost after the first request)
7071
- **JSON Output**: Easy integration with scripts and pipelines
7172
- **HED Schema Support**: Works with official HED schemas (8.x)
7273

@@ -83,6 +84,7 @@ The agents work in feedback loops, automatically refining the annotation until i
8384

8485
## Documentation
8586

87+
- [Prompt Caching and Usage Reporting](docs/prompt-caching.md) - What HEDit caches, what it saves, and where to see the numbers
8688
- [HED Standard](https://hedtags.org) - Learn about HED annotations
8789
- [GitHub Issues](https://github.com/Annotation-Garden/HEDit/issues) - Report bugs or request features
8890

@@ -234,6 +236,7 @@ uvicorn src.api.main:app --reload --host 0.0.0.0 --port 38427
234236
- `POST /annotate`: Generate HED annotation from natural language
235237
- `POST /validate`: Validate HED annotation
236238
- `GET /health`: Health check
239+
- `GET /metrics`: Token use, cost, and prompt-cache savings since startup (server API key required)
237240
- API URL: `http://localhost:38427`
238241

239242
## Development

SECURITY.md

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,9 +123,11 @@ See [`deploy/SECURITY.md`](deploy/SECURITY.md) for complete security documentati
123123
## Known Security Considerations
124124

125125
### LLM API Keys
126-
- OpenRouter API keys are stored as environment variables
127-
- Keys are passed to OpenRouter API via HTTPS
126+
- Anthropic API keys are stored as environment variables (`ANTHROPIC_API_KEY`,
127+
plus `ANTHROPIC_BASE_URL` and `ANTHROPIC_WORKSPACE_ID` for the Claude Platform on AWS)
128+
- Keys are sent to the Anthropic Messages API over HTTPS
128129
- Keys are not logged in audit logs
130+
- BYOK keys supplied via `X-Anthropic-Key` are used for that request only, never stored
129131
- Rotate keys if compromised
130132

131133
### Cloudflare Worker Proxy
@@ -134,11 +136,11 @@ See [`deploy/SECURITY.md`](deploy/SECURITY.md) for complete security documentati
134136
- Not visible in frontend JavaScript
135137
- Recommended for production deployments
136138

137-
### Local GPU Deployment
138-
- Ollama runs locally (no API key needed)
139-
- No external API calls for LLM inference
140-
- Complete privacy for offline operation
141-
- Ensure GPU drivers are up to date
139+
### Data Sent to the LLM
140+
- Event descriptions and images submitted for annotation are sent to Anthropic
141+
- Server mode bills the operator's Claude Platform on AWS workspace; BYOK mode
142+
bills the caller's own Anthropic account
143+
- Local inference is not supported; every annotation involves an external API call
142144

143145
## Security Scanning
144146

debug_workflow.py

Lines changed: 0 additions & 83 deletions
This file was deleted.

docs/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Welcome to the HED-BOT documentation! This directory contains comprehensive guid
2222
- **[Docker Updates](deployment/docker-updates.md)** - Updating Docker deployments
2323
- **[Docker Architecture](deployment/docker-architecture.md)** - Docker architecture details
2424
- **[Claude Platform on AWS](deployment/claude-platform-aws.md)** - Configuring the Anthropic Claude API via AWS Marketplace
25+
- **[Prompt Caching and Usage Reporting](prompt-caching.md)** - What caches, what it saves, and how to verify it
2526

2627
### 🛠️ Development
2728
- **[Contributing](development/contributing.md)** - How to contribute, issue labeling system

0 commit comments

Comments
 (0)