Skip to main content

๐ŸŒฒ 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.

PyPI version Python versions License: MIT CI Tests Token Savings PRs Welcome


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).

โ†’ Full benchmark details


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.


MIT License ยท Built for the age of autonomous coding agents

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

context_lineage-0.1.0.tar.gz (126.7 kB view details)

Uploaded Source

Built Distribution

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

context_lineage-0.1.0-py3-none-any.whl (107.5 kB view details)

Uploaded Python 3

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

Hashes for context_lineage-0.1.0.tar.gz
Algorithm Hash digest
SHA256 03ec50d6d72caef38340f917049398c3e13d5a60d8cae596a5e78a428b7c5e2b
MD5 d2e9adb133e568e65a08c0444e1cc3d9
BLAKE2b-256 7eab6d8208ef2aee61eea357203fcea2238eebeabe1d4876abad7baeeb3b2330

See more details on using hashes here.

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

Hashes for context_lineage-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ef2313b4a03a87f1860ff2da6a4e5270a0ba906bd531425cc57c2b2bce1cb4b
MD5 3a1b26a8ec8bcb0a7b7c697003e71e19
BLAKE2b-256 5289833e2545571bd3b6e29895b6ea7d7b3d4271a976c445800b4562336bf5c5

See more details on using hashes here.

Release history Release notifications | RSS feed

0.2.2

2 files

0.2.1

2 files

0.2.0

2 files

0.1.1

2 files

This release

0.1.0 This release

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page