Metadata-Version: 2.4
Name: reguliq-diagnostics
Version: 0.1.0
Summary: Diagnose context degradation in LLM agents — find where memory breaks and how to fix it
Project-URL: Homepage, https://github.com/Rishi-Bethi-007/context-lens
Project-URL: Repository, https://github.com/Rishi-Bethi-007/context-lens
Project-URL: Issues, https://github.com/Rishi-Bethi-007/context-lens/issues
Author-email: Rishi Bethi <rishibethi2001@gmail.com>
License: MIT License
        
        Copyright (c) 2026 Rishi Bethi
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
License-File: LICENSE
Keywords: agents,context-window,evaluation,langchain,langgraph,llm,rag,testing
Classifier: Development Status :: 3 - Alpha
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3.11
Classifier: Programming Language :: Python :: 3.12
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Classifier: Topic :: Software Development :: Testing
Requires-Python: >=3.11
Requires-Dist: anthropic>=0.52.0
Requires-Dist: python-dotenv>=1.0.0
Requires-Dist: tiktoken>=0.9.0
Provides-Extra: dev
Requires-Dist: ipykernel>=6.0.0; extra == 'dev'
Requires-Dist: jupyter>=1.0.0; extra == 'dev'
Requires-Dist: matplotlib>=3.8.0; extra == 'dev'
Requires-Dist: pandas>=2.2.0; extra == 'dev'
Requires-Dist: pytest-cov>=4.0.0; extra == 'dev'
Requires-Dist: pytest-mock>=3.0.0; extra == 'dev'
Requires-Dist: pytest>=7.0.0; extra == 'dev'
Requires-Dist: requests>=2.31.0; extra == 'dev'
Requires-Dist: seaborn>=0.13.0; extra == 'dev'
Requires-Dist: tqdm>=4.65.0; extra == 'dev'
Provides-Extra: langgraph
Requires-Dist: langchain-core>=0.3.0; extra == 'langgraph'
Requires-Dist: langgraph>=0.2.0; extra == 'langgraph'
Description-Content-Type: text/markdown

# context-lens

Tells you **where** your LLM agent's memory breaks, **at what token count**, and **how to fix it**.

```
pip install reguliq-diagnostics
```

---

## Quickstart

```python
from context_lens.engine.measurement import measure_context_health
from context_lens.reporter import Reporter

# 1. Probe your agent's context window
result = measure_context_health(
    agent_name="my-rag-agent",
    haystack=my_background_text,
    needle="The Q3 revenue was $4.2M",
    question="What was Q3 revenue?",
    expected="4.2M",
)

# 2. Run all 6 classifiers
report = Reporter().run(result)

# 3. View results
report.summary()          # terminal output
report.save("report.html")  # open in browser
```

---

## What it finds

| Pattern | What it means | Severity |
|---|---|---|
| `beginning_anchored` | Model retrieves facts only from the first 15% of context | HIGH |
| `cliff_detector` | Accuracy drops >20% between adjacent token counts | HIGH |
| `distractor_confusion` | Near-miss facts in context cause wrong answers | HIGH |
| `tool_burial` | Accuracy collapses after 3rd+ sequential tool call | MEDIUM |
| `instruction_drift` | System-prompt constraints weaken over conversation turns | MEDIUM |
| `recency_bias` | Model ignores everything except the last 20% of context | MEDIUM |

---

## Demo

### ReguliQ (production LangGraph agent) — healthy

> Instrumented with real LangGraph callbacks. Peak context: 965 tokens.
> At that scale, Claude Haiku retrieves with 100% accuracy.

```
context-lens: ReguliQ
  score: A  |  mean accuracy: 100.0%  |  5 classifiers run
  no patterns detected — context health looks good
```

[View reguliq_report.html](examples/reguliq_report.html)

### Synthetic unhealthy agent — context degradation detected

> Beginning-anchored retrieval + cliff at 30K tokens.

```
context-lens: my-rag-agent (synthetic)
  score: F  |  mean accuracy: 35.0%  |  5 classifiers run
  cliff: 30,000 tokens
  4 pattern(s) detected:
    [MEDIUM] beginning_anchored  conf=0.50
    [MEDIUM] cliff_detector      conf=0.58
    [HIGH  ] tool_burial         conf=0.62
    [HIGH  ] instruction_drift   conf=0.62
```

[View unhealthy_report.html](examples/unhealthy_report.html)

---

## Architecture

```
context_lens/
├── engine/
│   ├── probes.py          # NIAH probe injection + needle-in-haystack runs
│   ├── measurement.py     # sweeps positions × token counts, returns MeasurementResult
│   └── snapshots.py       # ContextSnapshot capture for live agents
│
├── classifiers/           # 6 pattern detectors (detect() + recommend())
│   ├── beginning_anchored.py
│   ├── cliff_detector.py
│   ├── distractor_confusion.py
│   ├── tool_burial.py
│   ├── instruction_drift.py
│   └── recency_bias.py
│
├── instrumentation/
│   └── langgraph.py       # LangGraphInstrumentor — wraps any compiled graph
│
├── reporter.py            # Reporter.run() → ReportData (score + recommendations)
│
└── report/
    ├── renderer.py        # renders ReportData → self-contained HTML (no CDN)
    └── template.html      # dark terminal theme, SVG charts, zero dependencies
```

### How it works

```
your agent          context-lens
──────────          ────────────────────────────────────
LangGraph    ──►  LangGraphInstrumentor
   graph           │  captures token counts per node
                   ▼
             measure_context_health()
                   │  plants NIAH probes at each
                   │  position × token count cell
                   ▼
             MeasurementResult
                   │  accuracy_by_position()
                   │  accuracy_by_token_count()
                   ▼
             Reporter.run()
                   │  runs all 6 classifiers
                   │  computes A-F grade
                   ▼
             ReportData.save("report.html")
```

---

## Installation

```bash
# Core (probing + classifiers + HTML report)
pip install reguliq-diagnostics

# LangGraph instrumentation
pip install "reguliq-diagnostics[langgraph]"

# Development
pip install "reguliq-diagnostics[dev]"
```

---

## Running the demos

```bash
# Unhealthy agent (synthetic — no API key needed)
python examples/unhealthy_agent_demo.py

# ReguliQ (requires API keys + ReguliQ repo)
python examples/reguliq_demo.py

# ReguliQ with Phase 3 baseline only (no API calls)
python examples/reguliq_demo.py --synthetic
```

---

## Dev

```powershell
# Setup (Windows)
uv venv && .venv\Scripts\activate
uv pip install -e ".[dev,langgraph]"

# Test
pytest tests/ -v --cov=context_lens

# Build
uv build
```

![209 tests passing](https://img.shields.io/badge/tests-209%20passing-brightgreen)

---

## License

MIT
