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: MIT CI

ContextOps analyzes the context fed into your LLM and tells you what's broken — redundant chunks, wasted tokens, structural imbalance — with a deterministic 0–100 score and actionable fixes.

Think of it as ESLint for your LLM prompts.


Why ContextOps?

Most LLM applications blindly stuff context into the prompt window. This leads to:

  • 💸 Wasted spend — paying for redundant tokens that don't improve output
  • 🔁 Silent regressions — a "small RAG change" floods the context with duplicates
  • 🏗️ Structural drift — retrieval chunks slowly dominate the entire prompt
  • 🎯 No visibility — teams have no way to measure context quality in CI

ContextOps gives you that visibility. It runs in your CI pipeline, scores every context payload, and fails the build if quality degrades.


Quick Start

pip install contextops

See it in action

# Run the built-in demo — instant "wow moment"
contextops demo

Analyze your own context

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

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

# Compare two snapshots for regressions
contextops diff before.json after.json

# JSON output for dashboards and 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}")

What It Measures

ContextOps computes a 0–100 Context Score from four independent penalty dimensions:

Dimension What It Detects Max Penalty
Redundancy Duplicate / near-duplicate chunks (N-gram + Jaccard) 30 pts
Density Wasted tokens from structural bloat 30 pts
Structure Imbalanced type distribution (e.g., retrieval > 70%) 20 pts
Concentration Source dominance or highly imbalanced chunk distribution 20 pts
Context Score = 100 - (Redundancy + Density + Structure + Concentration)

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


CI / CD Integration

GitHub Actions

name: Context Quality Gate

on: [pull_request]

jobs:
  context-check:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: actions/setup-python@v5
        with:
          python-version: "3.12"

      - run: pip install contextops

      - name: Check context quality
        run: contextops check prompts/context.json --min-score 75

Exit Codes

Code Meaning
0 Score meets threshold — build passes
1 Score below threshold — build fails

Regression Detection

# Save a baseline
contextops inspect prompts/v1.json --json-output > baseline.json

# After changes, compare
contextops diff baseline.json prompts/v2.json

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:

[
    {"role": "system", "content": "You are helpful."},
    {"role": "user", "content": "What is the refund policy?"}
]

CLI Reference

Command Purpose
contextops inspect <file> Analyze and display results
contextops check <file> --min-score N CI gate with exit codes
contextops demo Built-in demo context
contextops stability <file> Deterministic stability report
contextops diff <file_a> <file_b> Compare two snapshots

Flags

Flag Commands Purpose
--json-output inspect, check Machine-readable JSON output
--min-score N check Minimum passing score (0–100)
--model <name> inspect, check Target model for cost estimation
--explain inspect, check Show detailed penalty reasoning
--config <file> inspect, check Custom threshold config file

Design Principles

  1. Deterministic — Same input → same output. Always. No randomness, no embeddings, no LLM calls.
  2. Explainable — Every penalty maps to a real issue with a token count and a fix.
  3. CI-native — Designed for pipelines first. Exit codes, JSON output, threshold gating.
  4. Zero network — Runs entirely offline. No API keys, no external services.

Stability Contract

ContextOps ships with a formal Stability Contract that guarantees:

  • Scoring determinism — same input always produces the same score
  • Schema stability — JSON output fields never change within a major version
  • Performance bounds — sub-second for payloads up to 50,000 tokens
  • Semantic versioning — scoring formula changes require a major version bump

This contract exists so teams can trust ContextOps in production CI pipelines.


Development

# Clone and install in dev mode
git clone https://github.com/Abhijeet777/contextops.git
cd contextops
pip install -e ".[dev]"

# Run tests
pytest

# Run chaos stress tests
pytest tests/test_chaos.py -v

License

MIT

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.2.0.tar.gz (44.3 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.2.0-py3-none-any.whl (38.0 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for contextops-0.2.0.tar.gz
Algorithm Hash digest
SHA256 0d9aebc6e1b7c50afa70481460e08989b5d6e45b92f988865151d4b1cb4d3d92
MD5 b6592e059579ec2c17dd4020757462d6
BLAKE2b-256 35dc3b2fe68e79f55391de6f8e9730dca7780ad43cc27b466487f0c5d505abf1

See more details on using hashes here.

File details

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

File metadata

  • Download URL: contextops-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 38.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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1a3103f50e38bd33c1cca448dec57225ed6f3b12e81345b32440305a73d76a6a
MD5 c08edafc3dd9644aaf07d40f35af78c3
BLAKE2b-256 a22601aba723e3de86cf18cfc4a5910c6a24459d6de99581bfaa6c6a7485b2cb

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