๐ฒ ContextLineage
AST-Grounded Code Lineage & Progressive Context for AI Coding Agents
Stop burning 50,000+ tokens on full-codebase dumps. Give your agents progressive context, AST call graphs, and deterministic anti-hallucination guardrails.
Setup โ 2 Commands
pip install context-lineage
ctx setup src/
That's it. Claude Code, Cursor, and any other AI coding tool on your project will now automatically read a structured map of your codebase instead of guessing from raw source files.
Commit the generated files so every developer and every AI session uses them:
git add CLAUDE.md AGENTS.md .cursorrules
git commit -m "chore: add ContextLineage AI context"
What ctx setup generates
| File | Who reads it | What it contains |
|---|---|---|
CLAUDE.md |
Claude Code (automatic) | Module map, entry points, call graph, dependency contracts |
.cursorrules |
Cursor (automatic) | Same structured codebase context |
AGENTS.md |
Any AI agent | Instructions: use ctx verify before writing code, read CLAUDE.md first |
| Git pre-commit hook | Auto-runs on every git commit |
Keeps all files fresh โ zero maintenance |
Why This Exists
AI coding agents fail on real codebases in two ways:
Context Overflow โ Dumping all source files into the prompt burns 50,000+ tokens per turn, runs up huge API bills, and causes LLM reasoning to degrade ("Lost in the Middle" effect).
Context Starvation โ Reading only file names or unstructured docs causes agents to hallucinate non-existent functions, reversed caller/callee directions, and phantom circular dependencies.
ContextLineage fixes both. It builds a structured AST knowledge graph of your codebase and serves it progressively โ giving agents exactly what they need, nothing more.
What's Under the Hood
Your Python Codebase
โ
ctx setup src/
โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ 1. AST Dependency & Call Graph โ Who calls what, what imports what
โ 2. 4-Tier Progressive Disclosure โ Overview โ Summaries โ Symbols โ Source
โ 3. Anti-Hallucination Verifier โ verify_claim checks facts before writing
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ
CLAUDE.md / .cursorrules / AGENTS.md
โ
Claude Code / Cursor / Devin / Any Agent
Progressive Disclosure (how tokens are saved)
| Level | Content | Tokens |
|---|---|---|
| Level 0 | Architecture overview, entry points, leaf modules, cycle check | ~50 |
| Level 1 | Module purpose, key exports, direct dependencies | ~120 / module |
| Level 2 | Function signatures, caller/callee chains, semantic contracts | ~250 / symbol |
| Level 3 | Full source code โ loaded only for files being modified | Full |
Additional CLI Commands
# Auto-fix docstring drift when your code changes
ctx validate src/ --fix
# Verify a claim before writing code (prevents hallucinations)
ctx verify "run_pipeline calls extract_data" -d src/
# Understand impact before refactoring
ctx query impact pipeline.stages.extract -d src/
# Trace call chains through your codebase
ctx query lineage pipeline.orchestrator -d src/
# Manually regenerate context files at any time
ctx setup src/
Python API
from pathlib import Path
from contextlineage.agent_skill import create_code_explorer_skill
from contextlineage.context_packer import create_context_packer
skill = create_code_explorer_skill(Path("src/"), token_budget=8000)
skill.initialize()
# Verify a claim against AST ground truth
result = skill.verify_claim("run_pipeline calls extract_data")
# โ {"verified": True, "confidence": 0.95, "evidence": ["AST verified: ..."]}
# Pack context for LLM prompt injection
packer = create_context_packer(skill, max_tokens=8000)
packed = packer.pack_for_task("trace_dataflow", "pipeline.orchestrator")
prompt = f"Codebase context:\n{packed.to_markdown()}\n\nTask: {user_task}"
Real-World Benchmark
Evaluated on a 17-module real-world codebase:
| Task | Naive (All Files) | ContextLineage | Savings |
|---|---|---|---|
| Architecture Overview | 6,463 tokens | 1,806 tokens | 72% |
| Impact Analysis | 6,463 tokens | 3,288 tokens | 49% |
| Fact Verification | 6,463 tokens | 150 tokens | 97% |
| Average across tasks | 5,559 tokens | 2,058 tokens | 63% |
Anti-hallucination accuracy: 100% (6/6 true and false claims correctly classified by AST).
LangChain & CrewAI
# LangChain
from contextlineage.integrations.langchain import FrontmatterLoader, FrontmatterRetriever
loader = FrontmatterLoader("manifest.json")
retriever = FrontmatterRetriever(loader.manifest, k=5, token_budget=8000)
# CrewAI
from contextlineage.integrations.crewai import create_frontmatter_tools
tools = create_frontmatter_tools("manifest.json")
agent = Agent(role="Code Explorer", tools=tools)
dbt Support (Coming Soon)
ContextLineage is expanding to dbt SQL/Jinja models โ parsing {{ ref() }} and {{ source() }} DAGs, extracting column contracts from schema.yml, and providing the same progressive disclosure for dimensional modeling projects.
โ dbt Architecture Specification
Contributing
git clone https://github.com/swapnilwaramwar/ContextLineage.git
cd ContextLineage
pip install -e ".[dev]"
pytest tests/
See CONTRIBUTING.md for guidelines.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file context_lineage-0.1.0.tar.gz.
File metadata
- Download URL: context_lineage-0.1.0.tar.gz
- Upload date:
- Size: 126.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
03ec50d6d72caef38340f917049398c3e13d5a60d8cae596a5e78a428b7c5e2b
|
|
| MD5 |
d2e9adb133e568e65a08c0444e1cc3d9
|
|
| BLAKE2b-256 |
7eab6d8208ef2aee61eea357203fcea2238eebeabe1d4876abad7baeeb3b2330
|
File details
Details for the file context_lineage-0.1.0-py3-none-any.whl.
File metadata
- Download URL: context_lineage-0.1.0-py3-none-any.whl
- Upload date:
- Size: 107.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.13.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4ef2313b4a03a87f1860ff2da6a4e5270a0ba906bd531425cc57c2b2bce1cb4b
|
|
| MD5 |
3a1b26a8ec8bcb0a7b7c697003e71e19
|
|
| BLAKE2b-256 |
5289833e2545571bd3b6e29895b6ea7d7b3d4271a976c445800b4562336bf5c5
|