How Reasonix Optimizes Token Costs Using DeepSeek Prefix Cache

Reasonix slashes DeepSeek token costs by preserving immutable prefixes across API calls, ensuring cached tokens are billed at ~75% lower rates than fresh input tokens.

Reasonix is an open-source AI orchestration framework that exploits DeepSeek's prefix cache to minimize per-request expenses. DeepSeek's pricing model charges significantly less for tokens that hit the cache (e.g., ¥0.5 per 1M cached tokens versus ¥2 per 1M uncached tokens). By architecting conversations to maximize cache hits, Reasonix shifts the majority of token costs to the cheaper cached rate.

Understanding DeepSeek's Prefix Cache Mechanism

DeepSeek's prefix cache stores the system prompt and conversation history on the provider side using a cache key derived from the prefix content. When an identical prefix appears in subsequent requests, DeepSeek reuses the cached computation and charges the reduced cached-token rate.

The cache activates when:

  • The system prompt remains byte-for-byte identical across calls
  • The message sequence from the start of the conversation is preserved without modification
  • No intermediate tokens are inserted that would shift the prefix boundary

Only tokens after the cached prefix—typically new user queries—are billed at standard input rates.

How Reasonix Maximizes Prefix Cache Hits

Reasonix implements four core strategies to keep the prefix cache "warm" and minimize expensive uncached token consumption.

Stabilizing the System Prompt

Reasonix never mutates the system prompt mid-conversation. In internal/provider/provider.go, the provider-visible conversation structure maintains a fixed prefix:

// internal/provider/provider.go
// The system prompt and prior transcript form the prefix cache key.
// Changing either invalidates the cache and triggers full input pricing.
type Conversation struct {
	SystemPrompt string    // Immutable once established
	Transcript   []Message // Appended, never rewritten
}

This static system prompt ensures the cache key remains stable across all turns in a session.

Preserving Conversation Transcript Order

The guardian component in internal/guardian/guardian.go manages transcript integrity:

// internal/guardian/guardian.go
// system prompt and prior transcript stay in the prefix cache
type Guardian struct {
	systemPrompt string
	transcript   []Turn // Strict append-only, no reordering
}

By enforcing append-only semantics on the transcript, Reasonix guarantees that the cached prefix grows predictably. Each new turn extends the cacheable region rather than invalidating it.

Detecting Cache-Invalidating Changes

When sessions resume, Reasonix validates prefix continuity. In desktop/session_prompt.go, the system warns when changes would trigger a cache miss:

// desktop/session_prompt.go
func (sp *SessionPrompt) ValidateResume(newPrompt string) {
	if !strings.HasPrefix(newPrompt, sp.storedPrefix) {
		slog.Warn("desktop: resume swapped a differing system prompt; conversation prefix cache will miss")
		// Cache miss → all tokens billed at full input rate
	}
}

This detection prevents users from inadvertently doubling their token costs through configuration changes.

Warm-Up Persistence Across Restarts

The controller persists guardian state to maintain cache warmth. From internal/control/controller.go:

// internal/control/controller.go
func (c *Controller) Stop() {
	// Persist guardian session so prefix cache stays warm after restart
	state := c.guardian.Serialize() // Captures full prefix
	c.stateStore.Write(state)
}

func (c *Controller) Start() {
	state := c.stateStore.Read()
	c.guardian = NewGuardian(state) // Restores identical prefix
}

By serializing and restoring the complete conversation prefix, Reasonix ensures that restarts do not reset cache eligibility.

Practical Implementation Example

A typical Reasonix interaction demonstrates the cost-optimized flow:

// Initial request establishes cacheable prefix
const initialRequest = {
	model: "deepseek-v4-flash",
	system: "You are a cybersecurity analyst specializing in vulnerability assessment...",
	messages: [
		{ role: "user", content: "Analyze this log for threats." },
		{ role: "assistant", content: "I've identified three suspicious patterns..." }
	]
};

// Subsequent request reuses prefix, pays only for new tokens
const followUpRequest = {
	model: "deepseek-v4-flash",
	system: "You are a cybersecurity analyst specializing in vulnerability assessment...", // Identical → cache hit
	messages: [
		{ role: "user", content: "Analyze this log for threats." },
		{ role: "assistant", content: "I've identified three suspicious patterns..." },
		{ role: "user", content: "Dig deeper on pattern #2." } // Only these tokens billed at full rate
	]
};

The first two messages plus system prompt are retrieved from cache at cached-token pricing. Only the final user message incurs standard input costs.

Cache Hit Rate Impact on Costs

Scenario Token Pricing Relative Cost
Cold start (no cache) Full input rate (¥2 / 1M) 100% baseline
Prefix cache hit Cached rate (¥0.5 / 1M) 25% of baseline

For long conversations where 90% of tokens fall within the cached prefix, effective token costs approach the cached rate floor. Reasonix's architecture ensures this ratio remains stable across session lifetimes.

Key Source Files

Summary

  • Reasonix optimizes token costs by engineering conversations for maximum DeepSeek prefix cache utilization
  • Static system prompts eliminate cache key variation across requests
  • Append-only transcripts extend the cached prefix rather than invalidating it
  • Resume validation warns users when configuration changes would trigger expensive cache misses
  • State persistence maintains cache eligibility across application restarts

The result is predictable, minimized token pricing where the majority of conversation history is billed at DeepSeek's discounted cached-token rate.

Frequently Asked Questions

What happens if I change the system prompt mid-conversation?

Reasonix detects this in desktop/session_prompt.go and logs a warning: the prefix cache will miss and subsequent requests incur full input-token pricing for the entire conversation history. The system prevents automatic prompt mutation to avoid accidental cost spikes.

Does the prefix cache work across different DeepSeek models?

No. The cache key is model-specific. Reasonix maintains separate guardian sessions per model, and switching models (e.g., from deepseek-v4 to deepseek-v4-flash) creates a new cache context with cold-start pricing.

How much can Reasonix actually save on token costs?

Savings scale with conversation length. For a 50-turn conversation where 95% of tokens fall within the cached prefix, effective costs approach the cached-token rate (¥0.5 / 1M versus ¥2 / 1M), yielding approximately 75% reduction versus an equivalent uncached implementation.

Is prefix data stored locally or on DeepSeek's servers?

The cached computation resides on DeepSeek's infrastructure. Reasonix stores only the conversation state (system prompt and transcript) locally, serialized in internal/control/controller.go. This local state enables the client to recreate identical prefixes, triggering cache hits without transmitting redundant context.

Have a question about this repo?

These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →