Skip to main content

Scaffold-aware context compression for OpenAI-format messages — preserve state at 50%+ token reduction via local Ollama, OpenAI, or Anthropic

Project description

langstate

Scaffold-aware context compression for OpenAI-format messages. compress(messages) preserves all conversational state — facts, decisions, task status, user preferences — while reducing tokens by 50-54%.

Backed by the LPCI thesis: stateless LLMs hold state via language scaffold with transfer entropy approximately zero (Markov property). This is the productization of that proof.

What it does

from langstate import compress

compressed = compress(messages)
# Drop-in replacement: same OpenAI format, fewer tokens, state preserved
response = client.chat.completions.create(messages=compressed, model="gpt-4o")

The output is a valid OpenAI-format messages list:

  • System prompts kept verbatim
  • Last 4 turn-pairs kept verbatim
  • Older turns compressed into a [SCAFFOLD STATE] system message via local or cloud model

Install

# From GitHub (PyPI coming soon):
pip install git+https://github.com/hermes-labs-ai/langstate.git

Requirements: Python 3.10+, no heavy dependencies (stdlib only). For local summarization, run Ollama locally:

ollama pull qwen3:4b

Adapters

langstate ships three built-in summarizer backends:

Adapter Model Cost Key
local (default) qwen3:14b via Ollama zero none
openai gpt-4o-mini API OPENAI_API_KEY
anthropic claude-opus-4-7 API ANTHROPIC_API_KEY
from langstate import compress
from langstate.adapters import build

# Local Ollama (default, zero cost)
compressed = compress(messages)

# OpenAI
compressed = compress(messages, summarizer=build("openai"))

# Anthropic
compressed = compress(messages, summarizer=build("anthropic"))

# Any callable (prompt: str) -> str
compressed = compress(messages, summarizer=my_summarizer)

Configuration

compress(
    messages,
    preserve_recent=4,         # turn-pairs to keep verbatim (default: 4)
    min_turns_to_compress=6,   # skip compression for short conversations (default: 6)
    model="qwen3:4b",          # Ollama model when no summarizer is given
    summarizer=None,           # custom callable: (prompt: str) -> str
)

Adapter probe

from langstate.adapters import probe, REGISTRY

for name in REGISTRY:
    print(probe(name))
# {"name": "local", "available": True, "latency_ms": 423, ...}
# {"name": "openai", "available": False, "reason": "OPENAI_API_KEY not set", ...}

License

Apache-2.0


About Hermes Labs

Hermes Labs builds AI audit infrastructure for teams deploying AI agents in regulated environments. All tools are released as open-source software — MIT or Apache-2.0, no SaaS tier. The audit work is paid; the code is not.

hermes-labs.ai

OSS audit stack

Layer Tool Description
Static audit lintlang Agent-config static lint (HERM + H1-H7)
Static audit rule-audit Rule-logic audit: contradictions + gaps
Static audit scaffold-lint Scaffold budget + technique stacking
Static audit intent-verify Spec-drift checks
Runtime observability little-canary Prompt injection detection
Runtime observability suy-sideguy Runtime policy guard
Runtime observability colony-probe Prompt confidentiality audit
Regression & scoring hermes-jailbench Jailbreak regression benchmark
Regression & scoring agent-convergence-scorer N-agent output consistency
Supporting infra claude-router Model-tier + scaffold router
Supporting infra quickthink Compressed planning scaffold for local LLMs
Supporting infra langstate Scaffold-aware context compression
Supporting infra agent-gorgon Tool-fabrication defense for Claude Code
Supporting infra zer0dex Dual-layer agent memory
Supporting infra forgetted Mid-conversation incognito
Dev tools repo-audit Launch-readiness auditor
Dev tools quick-gate-python Python quality gate
Dev tools quick-gate-js JS/TS quality gate
Dev tools csv-quality-gate CSV preflight validation

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

langstate-0.1.0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

langstate-0.1.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file langstate-0.1.0.tar.gz.

File metadata

  • Download URL: langstate-0.1.0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for langstate-0.1.0.tar.gz
Algorithm Hash digest
SHA256 98d2b3d8a317b31aa31410e03739b06e58027d0529b85251b086ae93f846030b
MD5 4e9fe992c4e9518604f95cac4e3e10d6
BLAKE2b-256 3d8dbfa93cd4b394e47ae43025123559ec0155fe9b58f450dc18271012e58317

See more details on using hashes here.

File details

Details for the file langstate-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: langstate-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.3

File hashes

Hashes for langstate-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 78a5e8f47e6af6d78ba2a70225edf47f03e5b9e37223445704914f91171d2a89
MD5 01dd7a4860e21c6234ee1263a8a2c273
BLAKE2b-256 74cef31d87d317ba12df7ae2d43d286652324591531332f3a854e56d043aaa1e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page