Proxy Integration Plan
Goals
- Provide a single proxy service that acts as a drop-in replacement for both OpenAI and Anthropic APIs.
- Support OpenAI
/v1/chat/completions (route GPT-4o-mini → GPT-4o cascade).
- Support Anthropic
/v1/messages (route Haiku → Sonnet cascade).
- Preserve client compatibility with official SDKs, LangChain, and Claude Code.
- Maintain shared cascade logic, consistent telemetry, and per-provider cost tracking.
Reference MVP
- Existing MVP example:
examples/fastapi_proxy_routing.py (Anthropic-only). Extend it to support OpenAI format while sharing cascade logic.
Dual-Provider Architecture (Single Proxy)
- One FastAPI service with two router groups:
/v1/chat/completions → OpenAI-compatible handler.
/v1/messages → Anthropic-compatible handler.
- Shared core pipeline:
- Request normalization → cascade selection → provider dispatch → response normalization.
- Provider adapters implement the format translation, model mapping, and response shaping per standard.
Endpoint Mapping
OpenAI-Compatible
- Endpoint:
POST /v1/chat/completions
- Request mapping:
model: map GPT-4o-mini → GPT-4o cascade default.
messages: convert to internal message schema (role/content).
temperature, max_tokens, top_p, stream, etc.: pass through or apply defaults.
- Response mapping:
- Convert internal completion to
choices[0].message with role=assistant.
- Populate
usage and id per OpenAI response format.
Anthropic-Compatible
- Endpoint:
POST /v1/messages
- Request mapping:
model: map Haiku → Sonnet cascade default.
messages: convert to internal message schema (role/content).
max_tokens, temperature, top_p, stream, etc.: pass through or apply defaults.
- Response mapping:
- Convert internal completion to Anthropic
content array.
- Populate
usage and id per Anthropic response format.
Response Format Translation
- Internal canonical response:
content: assistant text
tool_calls: if supported
usage: prompt/completion/total tokens
model: resolved cascade model
request_id: proxy-generated ID
- OpenAI output:
choices = [{ index, message: { role: "assistant", content }, finish_reason }]
usage = { prompt_tokens, completion_tokens, total_tokens }
- Anthropic output:
content = [{ type: "text", text }]
usage = { input_tokens, output_tokens }
Shared Cascade Logic
- Central cascade policy used by both providers:
- Step 1: fast/cheap model (GPT-4o-mini or Haiku).
- Step 2: fallback to higher-quality model (GPT-4o or Sonnet) when confidence/quality checks fail.
- Confidence checks via existing scoring/validation hooks.
- Unified telemetry: log cascade steps, latency, and model decisions with provider tags.
Configuration (Default Models Per Provider)
- Env-driven defaults:
OPENAI_DEFAULT_MODEL=gpt-4o-mini
OPENAI_CASCADE_FALLBACK=gpt-4o
ANTHROPIC_DEFAULT_MODEL=claude-3-haiku
ANTHROPIC_CASCADE_FALLBACK=claude-3-sonnet
- Allow overrides per request, but enforce provider mapping logic.
Cost Tracking Per Provider
- Tag costs by provider and resolved model.
- Aggregate metrics:
provider=openai|anthropic
model=<resolved_model>
cascade_step=<primary|fallback>
- Emit metrics to existing cost tracking interfaces and examples.
Implementation Steps
- Create shared request/response normalization layer.
- Add OpenAI router and adapter for
/v1/chat/completions.
- Extend Anthropic handler to use shared cascade pipeline.
- Add response translation for both standards.
- Add provider-aware cost tracking and logging.
- Update examples to include OpenAI-compatible proxy usage.
Virtual Model Names
The proxy exposes virtual models that abstract cascade behavior:
| Virtual Model |
Behavior |
Use Case |
cascadeflow-auto |
Auto-select optimal cascade based on query complexity |
Default, balanced |
cascadeflow-fast |
Prioritize speed, accept more drafts |
Latency-sensitive |
cascadeflow-quality |
Prioritize quality, stricter thresholds |
Accuracy-critical |
cascadeflow-cost |
Maximum cost savings, aggressive draft acceptance |
Budget-constrained |
Usage
# Client just specifies virtual model
client = OpenAI(base_url="http://localhost:8000")
response = client.chat.completions.create(
model="cascadeflow-auto", # Proxy handles the cascade
messages=[{"role": "user", "content": "Hello"}]
)
Model Mapping
When proxy receives a virtual model:
- Parse cascade strategy from model name
- Select appropriate drafter/verifier pair
- Apply strategy-specific thresholds
- Return response with actual model used in metadata
Compatibility Notes
- OpenAI SDK: base URL override points to proxy.
- LangChain: use OpenAI-compatible route with environment variables.
- Anthropic SDK / Claude Code: base URL override points to proxy.
- Virtual models: work with any OpenAI-compatible client.
1---2name: 2817-proxy-integration-plan-da078dae3description: Proxy Integration Plan4---5# Proxy Integration Plan67## Goals8- Provide a **single proxy service** that acts as a drop-in replacement for both OpenAI and Anthropic APIs.9- Support **OpenAI** `/v1/chat/completions` (route GPT-4o-mini → GPT-4o cascade).10- Support **Anthropic** `/v1/messages` (route Haiku → Sonnet cascade).11- Preserve client compatibility with official SDKs, LangChain, and Claude Code.12- Maintain shared cascade logic, consistent telemetry, and per-provider cost tracking.1314## Reference MVP15- Existing MVP example: `examples/fastapi_proxy_routing.py` (Anthropic-only). Extend it to support OpenAI format while sharing cascade logic.1617## Dual-Provider Architecture (Single Proxy)18- **One FastAPI service** with two router groups:19 - `/v1/chat/completions` → OpenAI-compatible handler.20 - `/v1/messages` → Anthropic-compatible handler.21- **Shared core pipeline**:22 - Request normalization → cascade selection → provider dispatch → response normalization.23- **Provider adapters** implement the format translation, model mapping, and response shaping per standard.2425## Endpoint Mapping2627### OpenAI-Compatible28- **Endpoint**: `POST /v1/chat/completions`29- **Request mapping**:30 - `model`: map GPT-4o-mini → GPT-4o cascade default.31 - `messages`: convert to internal message schema (role/content).32 - `temperature`, `max_tokens`, `top_p`, `stream`, etc.: pass through or apply defaults.33- **Response mapping**:34 - Convert internal completion to `choices[0].message` with `role=assistant`.35 - Populate `usage` and `id` per OpenAI response format.3637### Anthropic-Compatible38- **Endpoint**: `POST /v1/messages`39- **Request mapping**:40 - `model`: map Haiku → Sonnet cascade default.41 - `messages`: convert to internal message schema (role/content).42 - `max_tokens`, `temperature`, `top_p`, `stream`, etc.: pass through or apply defaults.43- **Response mapping**:44 - Convert internal completion to Anthropic `content` array.45 - Populate `usage` and `id` per Anthropic response format.4647## Response Format Translation48- **Internal canonical response**:49 - `content`: assistant text50 - `tool_calls`: if supported51 - `usage`: prompt/completion/total tokens52 - `model`: resolved cascade model53 - `request_id`: proxy-generated ID54- **OpenAI output**:55 - `choices = [{ index, message: { role: "assistant", content }, finish_reason }]`56 - `usage = { prompt_tokens, completion_tokens, total_tokens }`57- **Anthropic output**:58 - `content = [{ type: "text", text }]`59 - `usage = { input_tokens, output_tokens }`6061## Shared Cascade Logic62- **Central cascade policy** used by both providers:63 - Step 1: fast/cheap model (GPT-4o-mini or Haiku).64 - Step 2: fallback to higher-quality model (GPT-4o or Sonnet) when confidence/quality checks fail.65- **Confidence checks** via existing scoring/validation hooks.66- **Unified telemetry**: log cascade steps, latency, and model decisions with provider tags.6768## Configuration (Default Models Per Provider)69- **Env-driven defaults**:70 - `OPENAI_DEFAULT_MODEL=gpt-4o-mini`71 - `OPENAI_CASCADE_FALLBACK=gpt-4o`72 - `ANTHROPIC_DEFAULT_MODEL=claude-3-haiku`73 - `ANTHROPIC_CASCADE_FALLBACK=claude-3-sonnet`74- **Allow overrides** per request, but enforce provider mapping logic.7576## Cost Tracking Per Provider77- **Tag costs** by provider and resolved model.78- **Aggregate metrics**:79 - `provider=openai|anthropic`80 - `model=<resolved_model>`81 - `cascade_step=<primary|fallback>`82- **Emit metrics** to existing cost tracking interfaces and examples.8384## Implementation Steps851. Create shared request/response normalization layer.862. Add OpenAI router and adapter for `/v1/chat/completions`.873. Extend Anthropic handler to use shared cascade pipeline.884. Add response translation for both standards.895. Add provider-aware cost tracking and logging.906. Update examples to include OpenAI-compatible proxy usage.9192## Virtual Model Names9394The proxy exposes **virtual models** that abstract cascade behavior:9596| Virtual Model | Behavior | Use Case |97|---------------|----------|----------|98| `cascadeflow-auto` | Auto-select optimal cascade based on query complexity | Default, balanced |99| `cascadeflow-fast` | Prioritize speed, accept more drafts | Latency-sensitive |100| `cascadeflow-quality` | Prioritize quality, stricter thresholds | Accuracy-critical |101| `cascadeflow-cost` | Maximum cost savings, aggressive draft acceptance | Budget-constrained |102103### Usage104```python105# Client just specifies virtual model106client = OpenAI(base_url="http://localhost:8000")107response = client.chat.completions.create(108 model="cascadeflow-auto", # Proxy handles the cascade109 messages=[{"role": "user", "content": "Hello"}]110)111```112113### Model Mapping114When proxy receives a virtual model:1151. Parse cascade strategy from model name1162. Select appropriate drafter/verifier pair1173. Apply strategy-specific thresholds1184. Return response with actual model used in metadata119120## Compatibility Notes121- **OpenAI SDK**: base URL override points to proxy.122- **LangChain**: use OpenAI-compatible route with environment variables.123- **Anthropic SDK / Claude Code**: base URL override points to proxy.124- **Virtual models**: work with any OpenAI-compatible client.