Cost Optimization Architecture

Intelligence Handoff Protocol

Stop paying GPT-4 prices for 'Hello World'. Use cheap models for chat and route only complex tasks to smart models. Use Ctxfy to transfer context between them instantly and cheaply.

The 'All-or-Nothing' Dilemma

The Expensive Way

Using GPT-4 for everything. Great intelligence, but you burn $10 per user just on chit-chat and simple queries.

The Dumb Way

Using Llama-70b for everything. Fast and cheap, but it fails on complex reasoning or coding tasks.

The Router Way: Use both. Ctxfy is the bridge.

The Handoff Pipeline

01

Front-Line Model

User chats with Claude Haiku (Cheap). Fast responses for general questions.

02

Intent Trigger

Router detects complex intent (e.g., 'Refactor this code'). Handoff initiated.

03

Ctxfy Compression

History is compressed into a 500-token State Object. No need to send 20k raw tokens.

04

Smart Model Execution

GPT-4 receives the State + New Prompt. Solves the task with full context but 95% less cost.

The Token Tax Savings

Cost of switching a 30k token conversation to GPT-4o:

Raw Handoff
$0.15
per switch
Ctxfy Handoff
$0.01
per switch
router.ts
async function handleUserMessage(history, newMessage) {
  // 1. Cheap Classifier
  const intent = await classifier.detect(newMessage);

  if (intent === 'SIMPLE') {
    return haiku.chat(history, newMessage);
  } 

  // 2. Complex Task -> Handoff
  if (intent === 'COMPLEX') {
    // Create source with CONTENT, receive postback with processed data
    const res = await fetch(`/v1/spaces/${spaceId}/sources`, {
      method: "POST",
      body: JSON.stringify({ type: "CONTENT", initial_data: { data: formatHistory(history) }, instructions: {} })
    });
    const { data } = await res.json();
    const source = await waitForPostback(data.id);
    return gpt4.complete({ system: `Context: ${source.content}`, prompt: newMessage });
  }
}

Just 5 Lines of Code

Implementing a router doesn't require complex infrastructure. It's a simple conditional check in your backend logic.

  • Detect Intent
  • Compress History (Ctxfy)
  • Call Expensive Model

Frequently asked questions

Cheap models are bad summarizers. They miss nuance, hallucinate facts, or lose code constraints. Ctxfy uses a specialized pipeline guaranteed to preserve state variables.

The compression happens in parallel or effectively instantaneously compared to the inference time of a large reasoning model like Opus or GPT-4.

Yes. Start in Anthropic, switch to OpenAI, finish in Local Mistral. The Ctxfy State Object is universal natural language.

Route smarter, not harder.

Combine the speed of Haiku with the brain of Opus.