v0.4.0 · Research-grade · Apache-2.0

Context,
engineered.

ceng distills the strongest context-engineering research of the past two years into four production-shaped Python primitives — compression, self-consistency checks, evolving playbooks, and portable OKF bundles. One pip install. No framework lock-in.

gpt-4o-mini
1  from ceng import ppa_compress

3  small = ppa_compress(
4      messages,              # longest user message
5      budget_tokens=2000,
6      llm="gpt-4o-mini",
7      cache_dir=".ceng/cache",
8  )

10 # 260k tokens → 2.1k golden summary
11 print(small.tokens)     # 2103
sqlite cache · 94% hit deterministic replay

Pipeline

messages chat history
partition_text 16 leaves
summarise_leaf ×N cache-hit fast path
combine_summaries aggregate → golden
OKF bundle index.md + combined.md
Self-consistency verified -99.2% tokens

Built on the research you can cite

arXiv:2607.15277 Partition, Prompt, Aggregate arXiv:2510.04618 Agentic Context Engineering arXiv:2510.26493 Context Engineering 2.0 Google Cloud 2026 Open Knowledge Format Anthropic 2025 Effective Context Engineering

Why ceng

Context is the constraint.
Treat it like one.

The strongest context-engineering papers of the last two years converged on the same insight: a good prompt is only as good as the context it stands on. ceng packages that insight into primitives you can reason about.

Treat memory as a first-class resource

Finite context is the precious resource. Every ceng strategy starts from the U-shape retention curve — keep what models actually use, compact the rest.

Research, not folklore

Each primitive is pinned to a specific paper, with recommended hyperparameters and verbatim prompts where the authors shipped them.

Primitives, not a framework

Every entry point is a plain synchronous function. No agents, no orchestration, no lock-in — call it from any stack you already run.

Dependency-light by design

Two runtime dependencies. Third-party SDKs load lazily, and transient failures recover with exponential backoff plus jitter.

Context retention · U-shape

compact_messages
Primacy Recency summarised in one call start end

Kept verbatim

System prompt, opening turns, and the latest context — untouched.

Distilled away

The middle strip collapsed in a single backend call, with provenance.

The primitives

Four primitives. Zero ideology.

Every function is synchronous, return-typed, and cache-aware. Compose them, or call one — they stand alone and disappear into your codebase.

ppa_compress

PPA Compression

Partition a long message, summarise each chunk in isolation, then aggregate. Macro-fallacy-resistant by construction.

ppa_compress(messages, budget_tokens=2000)
ppa_check

Self-consistency checks

Ask at the population level and per leaf of a binary tree, then compare. Catch macro fallacies before they ship.

ppa_check(question, population, tree)
ceng.playbook.Evolver

Evolving playbooks

ACE's Generator → Reflector → Curator loop with the paper's recommended hyperparameters baked in as defaults.

Evolver(llm=...).run(playbook, queries)
ppa_compress_to_okf

OKF bundles

Portable, on-disk context bundles with just-in-time retrieval. index.md + combined.md by default — nothing more.

ppa_compress_to_okf(messages, bundle_dir='ctx-out')
compact_messages

U-shape compaction

Preserve primacy and recency, summarise the middle in one call. The system role is never dropped or altered.

compact_messages(history, preserve_first=2, preserve_last=4)
ceng.notes.NotesManager

Agentic notes

Filesystem-backed external memory with atomic writes, LRU compaction, and pinned notes. No database, no daemon.

NotesManager(root='.ceng_notes').write(path, body)

All entry points are synchronous — no async, no streaming, no hidden agents to reason about.

In practice

Four lines. Every pipeline.

No builder objects. No context managers. Just named functions with sensible defaults and typed returns.

python -c "from ceng import ppa_compress"
from ceng import ppa_compress

messages = [
    {"role": "system", "content": "You are a careful analyst."},
    {"role": "user",   "content": "<very long context…>"},
]

small = ppa_compress(
    messages,
    budget_tokens=2000,    # whatever fits the ceiling
    llm="gpt-4o-mini",    # any litellm model id
    cache_dir=".ceng/cache",
)

260k tokens in, a golden summary out — leaves summarised in isolation, combined without macro fallacies.

python -c "from ceng import ppa_check"
from ceng import ppa_check

verdict = ppa_check(
    question="What fraction of users prefer feature X?",
    population="All of our users",
    tree=[
        {"description": "EU users", "prior": 0.3, "children": [
            {"description": "German users", "prior": 0.5},
            {"description": "French users", "prior": 0.5},
        ]},
        {"description": "US users", "prior": 0.7},
    ],
    llm="gpt-4o-mini",
)

print(verdict.self_consistent)  # False ⇒ macro fallacy

Population-level and per-leaf answers are compared automatically. A mismatch is your early warning.

python -m ceng.playbook.evolver
from ceng.playbook import empty_playbook
from ceng.playbook.evolver import Evolver

ev = Evolver(llm="gpt-4o-mini", cache_dir=".ceng/cache")
playbook, stats = ev.run(
    playbook=empty_playbook(),
    queries=train_samples,    # {"question", "ground_truth"}
    evaluator=lambda q, a, s: "correct" if a == s["ground_truth"] else "wrong",
    max_iterations=5,
)
# Generator → Reflector → Curator with ADD / UPDATE / MERGE / DELETE

Paper defaults out of the box: 5 reflector rounds, 0.90 dedup, 80k-token playbook budget.

python -m ceng.compact
from ceng.compact import compact_messages

small, prov = compact_messages(
    long_chat_history,
    preserve_first=2,    # keep system + first user
    preserve_last=4,     # keep the most recent turns
    summarise_middle=True,  # one backend call for the strip
    llm="gpt-4o-mini",
)
print(prov.summarised_count)  # provenance for your logs

The system role is never dropped or altered — a ValueError guards it at the boundary.

0

research-grade primitives

compress · check · evolve · store

0 +

tests, fixture-driven

no live LLMs in the suite

0

backends, one interface

litellm · openai · vllm

0

runtime dependencies

litellm + pyyaml, nothing else

Get started

In your environment in ten seconds.

Python 3.10 – 3.12. One import, typed returns, silent-by-default logging. Apache-2.0, so ship it anywhere.

$ pip install ceng

litellm + pyyaml — the base install

$ pip install ceng[tokenize]

adds exact tiktoken counting

$ pip install ceng[openai]

raw openai.OpenAI client, leaner

$ pip install ceng[vllm]

in-process vLLM on your GPU

No telemetry No framework OPENAI_API_KEY · ANTHROPIC_API_KEY · CENG_BACKEND
Your first compress quickstart.py
import ceng

ceng.set_backend("litellm")     # openai · vllm also built in

from ceng import ppa_compress

small = ppa_compress(
    messages,
    budget_tokens=2000,
    llm="gpt-4o-mini",   # or any model you can name
    cache_dir=".ceng/cache",
)

print(f"{len(messages)} messages → {budget_tokens}-token summary")
pip-friendly poetry-friendly uv-friendly venv-friendly conda-friendly 0.4.0 · stable API

Enough reading

Stop managing context.
Start engineering it.

Whatever you build — agents, RAG, evals, long-running chats — ceng gives you the researched primitives to make your tokens count.

Apache-2.0 · no telemetry · no hidden agents