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.1.tar.gz (127.1 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.1-py3-none-any.whl (107.9 kB view details)

Uploaded Python 3

File details

Details for the file context_lineage-0.1.1.tar.gz.

File metadata

  • Download URL: context_lineage-0.1.1.tar.gz
  • Upload date:
  • Size: 127.1 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.1.tar.gz
Algorithm Hash digest
SHA256 624d5ad8cb864b9917744e9ea88e936e0c2d20f63dc8a3ce6c61d0142e90579e
MD5 2a669a2617498a0e4e89751dca5f032e
BLAKE2b-256 546e7694523d01802af58ec5b729d1f3f96011887775e9f78bca2bda5a7ea37c

See more details on using hashes here.

File details

Details for the file context_lineage-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: context_lineage-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 107.9 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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 4de2666c5622eccbffd1f3aa847af6809d62a9a79fb8f789321345ef6a9140a7
MD5 e8386973a9ba11a382a0f8e0f43c8760
BLAKE2b-256 3389278b53cd30290f35e29364f56e8c889f21ce6c08389012e417ce83a2cc1f

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

This release

0.1.1 This release

2 files

0.1.0

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