Skip to main content

Deterministic context linter for LLM applications — analyze, score, and optimize your LLM context payloads.

Project description

ContextOps

The deterministic context linter for LLM applications.

PyPI version Python License: Sustainable Use CI

ContextOps analyzes the context data you send to your LLM and tells you what is broken. It detects redundant information, wasted tokens, and structural imbalances, providing a deterministic 0-100 score and actionable fixes.

Think of it as a spell-checker for your AI prompts.


Why ContextOps?

Most AI applications blindly stuff data into the prompt window. This leads to:

  • Wasted spend: paying for redundant tokens that do not improve the AI's output.
  • Silent regressions: a small change in search logic floods the prompt with duplicates.
  • Structural drift: retrieved documents slowly take over the entire prompt, hiding the actual instructions.
  • No visibility: developers have no way to measure context quality in automated testing.

ContextOps gives you this visibility. It runs in your testing pipeline, scores every context payload, and fails the build if quality drops.


What is New in v0.3.0

  • The Roast Engine: Enable the --roast flag on the CLI to get brutally honest, score-band commentary on your context quality. It will tell you exactly how bad (or good) your context really is.
  • ContextBench: We have introduced ContextBench, a suite of 1,500 real-world enterprise context payloads to test and calibrate scoring algorithms.
  • JSON Output: The roast commentary is now included in the raw JSON output for easy integration into dashboards or leaderboards.

The ContextBench Leaderboard

The ContextBench Leaderboard is the definitive benchmark for evaluating LLM context optimization architectures. It tests optimizers across 1,500 enterprise RAG payloads and scores them using a weighted formula: 50% Quality + 35% Compression + 15% Latency.

To keep the competition honest, there is a hard Quality Floor Gate. If your optimizer destroys the context so badly that the ContextOps score drops below 78, you are instantly disqualified. You cannot win on compression alone.

How to Submit

Submission is completely automated via GitHub Actions:

  1. Write a Python function def optimize_context(context: dict) -> dict: in a public GitHub repository.
  2. Open a new Issue in this repository titled Submission: [Your Team Name].
  3. Put the link to your public repository in the issue body.

Our automated harness will clone your code, run it against ContextBench, enforce the quality gates, generate a ContextOps Roast, and automatically push your score to the live leaderboard.


Quick Start

pip install contextops

See it in action

# Run the built-in demo to see a live analysis, complete with the Roast Engine
contextops demo

Analyze your own context

# Full analysis with rich terminal output
contextops inspect context.json

# Add the roast flag for honest commentary
contextops inspect context.json --roast

# CI mode: fail if the score drops below a threshold
contextops check context.json --min-score 70

# Compare two snapshots to find regressions
contextops diff before.json after.json

# JSON output for automation
contextops inspect context.json --json-output

Python API

from contextops.api.inspect import inspect_context

result = inspect_context({
    "system": "You are a helpful assistant.",
    "chunks": [
        {"content": "Refund policy: 30 days...", "source": "docs/refund.md"},
        {"content": "Refund policy: within 30 days...", "source": "docs/refund.md"},
    ],
    "memory": ["User asked about refunds before."],
})

print(f"Score: {result.score}/100")
print(f"Wasted tokens: {result.token_breakdown.wasted_tokens}")
for rec in result.recommendations:
    print(f"  -> {rec.fix}")

LangChain Integration

ContextOps plugs into any LangChain chain with a single line. We use explicit injection rather than hidden global patches, so you always know exactly where ContextOps is running.

pip install contextops langchain-core
from contextops import ContextOps

# Attach to any chain
chain = chain.with_config({
    "callbacks": [ContextOps.auto()]
})

result = chain.invoke({"question": "What is the refund policy?"})

Output is printed automatically before the LLM execution begins. You can also configure it to warn or block execution if the score is too low:

# Block execution entirely if context quality is below 70
ContextOps.auto(mode="block", min_score=70)

What It Measures

ContextOps computes a 0-100 Context Score based on four independent penalties:

Dimension What It Detects Max Penalty
Redundancy Duplicate or near-duplicate chunks of text. 30 pts
Density Wasted tokens from structural bloat and formatting. 30 pts
Structure Imbalanced data distribution (e.g., retrieval taking up 80% of the prompt). 20 pts
Concentration Over-reliance on a single document or source. 20 pts

Every penalty maps to a specific finding with token savings and an actionable fix.


Context File Format

ContextOps accepts a JSON file with any combination of these keys:

{
    "system": "Your system prompt here",
    "messages": [
        {"role": "user", "content": "User question"}
    ],
    "chunks": [
        {"content": "Retrieved chunk text", "source": "docs/page.md"}
    ],
    "memory": [
        "Previous conversation context"
    ],
    "tools": [
        {"name": "search_api", "output": "Tool response text"}
    ]
}

It also accepts raw OpenAI message lists directly.


Stability Contract

ContextOps ships with a formal Stability Contract (STABILITY.md) that guarantees:

  • Scoring determinism: the same input always produces the same score.
  • Schema stability: JSON output fields never change within a major version.
  • Semantic versioning: any changes to the scoring formula require a major version bump.

This ensures you can trust ContextOps in your production testing pipelines.


License

Sustainable Use License (see LICENSE file for details).

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

contextops-0.3.1.tar.gz (80.2 kB view details)

Uploaded Source

Built Distribution

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

contextops-0.3.1-py3-none-any.whl (62.0 kB view details)

Uploaded Python 3

File details

Details for the file contextops-0.3.1.tar.gz.

File metadata

  • Download URL: contextops-0.3.1.tar.gz
  • Upload date:
  • Size: 80.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for contextops-0.3.1.tar.gz
Algorithm Hash digest
SHA256 7c13eb3a4679242107b71798a3612577965e39d1f143eb908b7f07317af859a5
MD5 ab743a1258fab92558783e958fef27c0
BLAKE2b-256 b14761169943f02e2b785257c02022e6454f0b2979ae6aaaa55d815b559f0543

See more details on using hashes here.

File details

Details for the file contextops-0.3.1-py3-none-any.whl.

File metadata

  • Download URL: contextops-0.3.1-py3-none-any.whl
  • Upload date:
  • Size: 62.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.2

File hashes

Hashes for contextops-0.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 0e61101b9450639b7dbab037ba5685ae49ddd650b01481948ba1763d3ccbae6d
MD5 63085b9ebd27809d0f3d576222562472
BLAKE2b-256 e82737e46ecec13af83e7f27ae47b944e37dbf6e78c8f42000d28e417cc80168

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