Skip to main content

HiveState — Adaptive State Compression

HiveState is ModelHive's adaptive state compression middleware that automatically compresses long conversation histories into structured JSON state representations. When token counts exceed a configurable threshold, HiveState replaces the full message history with a compact "state snapshot" that preserves the minimum sufficient information for the LLM to continue the conversation correctly.

Key insight: HiveState preserves executable workflow state, not history — extracting intent, identifiers, action outcomes, and pending requirements while eliminating redundant context.

Why HiveState?​

Without CompressionWith HiveState
Token costs grow linearly with conversation lengthLong histories compressed to compact state
Context window limits restrict conversation depthConversations can run indefinitely
Manual summarization requires code changesZero-change integration via headers
Lost context when truncatingStructured extraction preserves critical state

Real-World Results​

DomainActivation RateMean Token ReductionQuality Score
Customer Service85%54.1%4.74/5.0
Tool/Function Agents87%46.6%4.50/5.0
Coding Agents93%71.5%4.80/5.0
Web Agents98%60.1%4.76/5.0

How It Works​

Processing Pipeline​

  1. Threshold Gate — Below-threshold requests pass through with zero overhead
  2. Dynamic Profiling — Classifies conversations as tool-agent, code-agent, or chat without LLM calls
  3. Conversation Zoning — Splits messages into System (unchanged), History (compressed), Recent (preserved verbatim), Last (final user message)
  4. Importance Scoring — Ranks messages by Recency, Error Signals, Decision Density, and Information Density
  5. State Extraction — An extraction model produces a structured JSON state with intent, constraints, progress, and errors
  6. CCR Injection — Cache-Compress-Retrieve: original history is cached, keywords overlap triggers selective reinjection on subsequent requests

Zero Configuration​

HiveState works automatically when enabled. Just send requests to the ModelHive API and compression happens transparently for long conversations.

Request Headers​

HeaderValuesBehavior
x-hive-state"false"Opt-out of HiveState compression for this request

Response Headers​

Every response from a HiveState-eligible request includes diagnostic headers:

HeaderExampleDescription
x-hivestate-modestateProcessing mode: state (compressed) or none (pass-through)
x-hivestate-original-tokens7000Token count before compression
x-hivestate-tokens220Token count after compression
x-hivestate-ratio0.97Compression ratio (1 − result/original)
x-hivestate-latency-ms1215Extraction latency in milliseconds
x-hivestate-extraction-prompt-tokens3200Tokens consumed by the extraction prompt
x-hivestate-extraction-completion-tokens180Tokens generated by extraction
x-hivestate-intentexchange_itemsExtracted conversation intent
x-hivestate-constraints{"identifiers":{...}}JSON of active constraints
x-hivestate-fallbackbelow_threshold(350/4000)Reason if compression was skipped

Usage Examples​

from openai import OpenAI

client = OpenAI(
api_key="sk-your-modelhive-key",
base_url="https://api.modelhive.ai/v1"
)

# Long conversation — HiveState compresses automatically
messages = [
{"role": "system", "content": "You are a helpful coding assistant."},
{"role": "user", "content": "Create a React todo app with TypeScript"},
{"role": "assistant", "content": "I'll create a React TypeScript todo app..."},
# ... many more turns ...
{"role": "user", "content": "Now add drag-and-drop reordering"},
]

response = client.chat.completions.create(
model="gpt-4o",
messages=messages
)

# HiveState transparently compresses the history if above threshold
# Check response headers for compression metrics

Extracted State Schema​

When HiveState compresses a conversation, it produces a structured JSON state:

{
"intent": "exchange_items",
"difficulty": "standard",
"reasoning_effort": "medium",
"active_constraints": {
"identifiers": { "order_id": "ORD-9842", "user": "sophia_h" },
"values": { "item": "Blue Wool Sweater M", "reason": "wrong size" },
"actions_taken": [
{"tool": "lookup_order", "input": "ORD-9842", "result": "found, status: delivered", "success": true}
],
"files_modified": [
{"path": "src/app/page.tsx", "change": "created with shadcn Button import"}
],
"progress": {
"done": ["verified order", "confirmed eligibility"],
"current": "processing exchange request",
"next": ["generate return label", "send confirmation"]
},
"requirements": { "size": "L", "color": "same" },
"errors_encountered": [
{"error": "inventory_unavailable", "resolution": "suggested alternative color"}
]
},
"conversation_status": "in_progress"
}

Dynamic Conversation Profiling​

HiveState automatically classifies conversations into profiles that tune compression parameters:

ProfileDetection RuleBehavior
tool-agent>30% messages are tool responsesMaximizes tool context preservation
code-agent>20% messages contain code patternsHigher budget for code-heavy conversations
chatDefaultBalanced compression for general chat

Conversation Zoning​

Messages are split into zones with different treatment:

ZoneTreatment
SystemPassed unchanged (system prompts preserved)
HistoryCompressed into structured state
RecentLast N assistant steps preserved verbatim (configurable step_window)
LastFinal user message forwarded verbatim

Fallback Conditions​

HiveState gracefully falls back to pass-through when compression wouldn't help:

ReasonDescription
no_historyNo conversation history to compress
below_thresholdToken count below configured threshold
no_modelNo extraction model configured
insufficient_headroomRecent + Last zones already use >85% of original tokens
extraction_failedExtraction model returned an error
no_savingsCompressed result wouldn't save meaningful tokens

Supported Formats​

HiveState works transparently with both request formats:

EndpointFormatStep Splitting
POST /v1/chat/completionsOpenAITurn-based (preserves tool_calls/tool in recent window)
POST /v1/messagesAnthropicStep-based (preserves multi-turn sequences)

Context Recovery (CCR)​

Compressing a history means the verbatim content — a file the agent read, the output of a tool — is no longer in the prompt. CCR keeps that content aside and re-injects it into a later turn when the conversation refers to it again, so compression does not have to choose between a small prompt and a complete one.

The scope is a security boundary​

CCR holds original messages, so a retrieval that crossed callers would hand one tenant another tenant's source code. Everything is filed under a scope and is only ever retrievable under the identical scope:

scope = (team_id, key_hash, session_id)

There is no shared bucket and no global fallback. A scope needs both an owner — a team, or the key hash when the key has no team — and a session. A request that cannot produce both has CCR disabled: no memory at all is the safe failure, shared memory is not.

The partition key is a SHA-256 of the three components, each length-prefixed rather than delimited. That is deliberate: session_id comes straight from a client header, and with a delimiter — even NUL — a caller could pick a session name that hashes into someone else's partition. Length prefixes make the encoding injective for any input.

How the session is decided​

InputSession used
x-hive-session headerTaken verbatim
No headerDerived by hashing the system and developer messages plus the first user turn
Neither header nor any system/user messageNone — CCR is off for that request

The derived anchor is closed by the first user turn, so it stays byte-stable as the conversation grows while differing between conversations. CCR therefore works without any client change; the header is there for callers that want to control the boundary themselves — for instance to keep two parallel threads of the same key apart, or to deliberately start fresh.

curl https://api.modelhive.ai/v1/chat/completions \
-H "Authorization: Bearer sk-mh-..." \
-H "x-hive-session: ticket-4471" \
-d '{"model": "gpt-4o", "messages": [...]}'

Bounds​

Storage is bounded twice, so one tenant cannot displace another:

  • each scope holds at most 32 files and evicts its own oldest first;
  • at most 512 scopes are live, evicting the least recently used.

Entries expire after 30 minutes. The decay counter is per scope — content leaves the working set after 5 turns without being mentioned — so unrelated traffic cannot age another scope's working set out.

Best Use Cases

HiveState provides the highest savings for long-running agent conversations — coding assistants, multi-step tool agents, and customer service bots with extended interaction histories.

Extraction Cost

The extraction call itself uses tokens (tracked as @hivestate provider spend). However, the net savings from reduced prompt tokens on the primary model typically exceed extraction costs by 3-10x.