Cortex Documentation

Cortex generates a knowledge base for your project that Claude Code reads automatically — before writing a single line of code.

Every time you start a Claude Code session, it knows nothing about your project. You end up re-explaining context: which parts break often, why something was written a certain way, what that refactor from last month was about.

Cortex solves this by analyzing your repository and writing structured context files to .claude/docs/. Claude Code reads these files automatically. The context travels with your code — commit it once and everyone on the team benefits.

What Cortex is not: It's not documentation you write by hand. It extracts knowledge that already exists in your git history, PR discussions and code structure.

Installation

One-line install (recommended)

curl -fsSL https://raw.githubusercontent.com/kovalenko-tech/cortex/main/install.sh | bash

Installs cortex to ~/.local/bin/cortex. Requires Python 3.11+ and git.

With npx

npx @kovalenko-tech/cortex-ai analyze

No installation needed. Node.js checks for Python 3.11+ and installs cortex via pip automatically.

Manual install

pip install git+https://github.com/kovalenko-tech/cortex.git

With security extras

pip install "git+https://github.com/kovalenko-tech/cortex.git#egg=cortex[security]"

Installs bandit for Python security analysis.

Verify installation: Run cortex --version to confirm it's installed correctly.

Quick Start

1

Run the setup wizard

The easiest way to get started. Walks you through analysis, CLAUDE.md generation, MCP config, and git hook installation.

cd your-project
cortex setup
2

Or run analysis directly

cortex analyze

Analyzes your entire project. Takes 30–120 seconds depending on project size. Writes context to .claude/docs/.

3

Commit the context

git add .claude/
git commit -m "add cortex context"

Context travels with your repo. Everyone who clones it gets the full project history.

4

Configure Claude Code (optional but recommended)

Add Cortex as an MCP server so context is injected automatically:

// .claude/settings.json
{
  "mcpServers": {
    "cortex": {
      "command": "cortex",
      "args": ["mcp"]
    }
  }
}

How it works

Cortex runs four analysis passes on your repository and combines the results into per-file context documents.

1. Git History Mining

For every source file, Cortex reads the full git log and extracts bug fixes, refactors, and the commit messages that explain why things changed.

2. Co-change Analysis

Builds a matrix of which files changed together across all commits. If auth.py and session.py changed together in 80% of commits, they're marked as related.

3. Static Analysis

Parses each file's AST to extract functions, classes, imports, and which test files cover the module.

4. Security Scan

Runs SAST tools (bandit for Python, semgrep for others) per file and scans git history for accidentally committed secrets.

Results are combined into .claude/docs/src/auth.py.md — one file per source file. Claude Code reads these before making changes.

Context files

Cortex writes all context to .claude/docs/ in your project root. The structure mirrors your source tree:

.claude/
  docs/
    src/
      auth.py.md
      models/user.py.md
      api/routes.py.md
    SUMMARY.md    ← project overview + risk
    SECURITY_REPORT.md ← all issues in one place
    DEPENDENCIES.md   ← vulnerable packages
  get_context.py     ← helper for manual use
  settings.json      ← MCP config
CLAUDE.md           ← project overview for Claude

Per-file context format

Each .md file follows this structure:

# src/auth.py

> ⚡ Fresh — analyzed 2026-03-21 UTC
> 🔴 HIGH RISK (score: 65/100) — 12 bug fixes · no test coverage

## Overview
Authentication module. Handles login, token validation, session management.

## Historical Insights
- [Bug Fix] 2024-03-15: Session fails on UTC+0 timezone
  Fix: Always use datetime.timezone.utc

## Decisions & Context
- ✅ Chose JWT over sessions for stateless auth — PR #234 @john
- 🚫 Don't call authenticate() before db.init() — PR #156 @maria

## Edit Checklist
- Run: pytest tests/test_auth.py -v
- Check: SESSION_TIMEOUT in config/settings.py

## Related Files
- tests/test_auth.py [co-change: 92%]
- middleware/session.py [co-change: 67%]

## Security Notes
- ✅ No issues found

Commit .claude/ to your repo. Context travels with the code. New team members get full project history on day one.

Git History Mining

Cortex reads your entire git log and extracts structured insights per file. It classifies commits by type and links each insight to the file it affected.

Commit classification

Commits are classified using keyword patterns:

TypeKeywordsWhat it means
bug_fixfix, bug, hotfix, patch, resolve, crashSomething was broken and got fixed
refactorrefactor, cleanup, improve, simplify, renameCode was restructured
featureeverything elseNew functionality added

Only bug_fix and refactor commits appear in Historical Insights — feature commits are noise.

Co-change analysis

Cortex builds a co-change matrix from all commits. If files A and B changed together in many commits, they're listed as related. This reveals implicit coupling that isn't visible in imports.

# Co-change score formula
score = co_changes / min(changes_A, changes_B)
# score > 0.2 → files are related

Risk Scoring

Every file gets a risk score from 0 to 100 based on four factors:

FactorMax pointsLogic
Bug history3010+ bug fixes = 30pts, 5-9 = 20pts, 2-4 = 10pts
No test coverage25No test files found = 25pts
Security issues253+ issues = 25pts, 1-2 = 15pts
High churn1050+ commits = 10pts, 20-49 = 5pts
Many dependents1010+ files depend on this = 10pts

Risk levels

HIGH Score ≥ 50 — handle with care, check tests, review changes carefully

MEDIUM Score 25–49 — some risk factors present

LOW Score < 25 — safe to modify

Viewing risk

cortex risks              # all files sorted by risk score
cortex risks --level HIGH # only high-risk files
$ cortex risks

Risk Analysis — /Users/kyrylo/projects/myapp

🔴 High risk: 3
🟡 Medium risk: 8
🟢 Low risk: 36

Files requiring attention:
🔴 src/auth.py (score: 65) 12 bug fixes · no test coverage
🔴 api/payments.py (score: 55) 8 bug fixes · 2 security issues
🟡 models/user.py (score: 35) no test coverage · high churn

Freshness Tracking

Context files show when they were generated and how many commits happened since. This tells Claude Code whether to trust the context.

Freshness levels

⚡ FRESH Analyzed < 3 days ago and ≤ 2 commits since

⚠️ STALE Analyzed 3–14 days ago or 3–10 commits since

❌ OUTDATED Analyzed 14+ days ago or 10+ commits since

Checking freshness

cortex freshness              # all files
cortex freshness --stale-only # only problem files

Keeping context fresh

cortex install-hook            # auto-update on every commit
cortex watch                   # auto-update when commits arrive
cortex analyze --since HEAD~10 # refresh last 10 commits worth of files

Security Audit

Cortex runs SAST analysis per file and scans git history for leaked secrets. Results appear in each file's context and in .claude/docs/SECURITY_REPORT.md.

What's scanned

  • SQL injection, XSS, command injection
  • Hardcoded secrets (API keys, passwords, tokens)
  • Dangerous functions (eval, exec, pickle.loads)
  • Secrets in git history (GitHub tokens, OpenAI keys, AWS credentials)
  • Weak cryptography (MD5/SHA1 for passwords)

Tools used

LanguageTool
Pythonbandit (install with pip install cortex[security])
All languagessemgrep (if installed)
Git historyBuilt-in regex scanner
cortex security # audit only, no full analysis
cortex analyze  # security runs as part of full analysis

Dependency Scan

Checks your project dependencies for known vulnerabilities. Results written to .claude/docs/DEPENDENCIES.md.

cortex deps
LanguageToolWhat it checks
Pythonpip-auditPyPI packages against CVE database
Node.jsnpm auditnpm packages against npm advisory database
Flutter/Dartflutter pub outdatedOutdated pub packages

Note: pip-audit installs automatically if missing. npm audit requires Node.js. Flutter audit requires Flutter SDK.

PR Knowledge Mining

The most unique feature of Cortex. It mines merged GitHub PRs for implicit architectural decisions — the reasoning that lives in review comments but never makes it into code.

Setup

export GITHUB_TOKEN=ghp_...
cortex mine-prs
cortex analyze   # context now includes PR decisions

What it extracts

Cortex scans PR comments for these patterns:

IconTypePatternExample
Decisionchose/decided/going with because"We chose Redis because we need persistence"
🚫Warningdon't/avoid/never"Don't cache this — caused prod outage"
⚠️Riskwill break/careful/watch out"Will break if you change the token format"
💡Reasonthe reason/this is because/intentionally"This is intentional — the API requires it"

Output in context

## Decisions & Context
*Extracted from PR discussions:*

- ✅ Chose JWT over sessions for stateless auth across services
  [PR #234](https://github.com/...) — @john

- 🚫 Don't call authenticate() before db.init()
  [PR #156](https://github.com/...) — @maria

- ⚠️ Will break if you change the token expiry format
  [PR #312](https://github.com/...) — @alex

Saved data

Mined knowledge is saved to .claude/pr-knowledge.json. Run cortex analyze after cortex mine-prs to include it in context files.

AI Summaries

When ANTHROPIC_API_KEY is set, Cortex uses Claude Haiku to write a 2–3 sentence technical overview for each file. All other features work without an API key.

export ANTHROPIC_API_KEY=sk-ant-...
cortex analyze

The summary appears in the Overview section of each context file and describes what the file does, its main responsibility, and any notable risk areas.

Cost estimate: Claude Haiku costs ~$0.25 per million input tokens. A project with 100 files typically costs $0.05–0.20 for the summaries. Use --no-llm to skip if cost is a concern.

MCP Server

Cortex can run as an MCP (Model Context Protocol) server, allowing Claude Code to query context directly without reading .md files manually.

Configuration

Add to .claude/settings.json in your project:

{
  "mcpServers": {
    "cortex": {
      "command": "cortex",
      "args": ["mcp"]
    }
  }
}

Restart Claude Code. The MCP server exposes two tools:

ToolDescription
get_file_contextReturns full context for a specific file path
get_project_summaryReturns SUMMARY.md and SECURITY_REPORT.md

The MCP server reads from .claude/docs/. Make sure you've run cortex analyze first.

Commands

Setup & Analysis

CommandDescription
cortex setupInteractive wizard — analyze, CLAUDE.md, MCP, git hook
cortex analyzeFull project analysis
cortex initGenerate CLAUDE.md with project overview
cortex mine-prsMine GitHub PR comments for decisions

Inspect

CommandDescription
cortex context <file>Show context for a file
cortex statusAnalysis status, freshness, risk summary
cortex risksFiles by risk score
cortex freshnessContext staleness per file
cortex securitySecurity audit only
cortex depsDependency vulnerability scan

Maintenance

CommandDescription
cortex diff <branch>Update context for files changed vs branch
cortex install-hookInstall git pre-commit hook
cortex watchWatch for commits and auto-update
cortex cleanRemove all context files and cache

Integration

CommandDescription
cortex mcpStart MCP server

Flags

FlagCommandDescription
--since <ref>analyzeOnly files changed since git ref (HEAD~10, main, 2024-01-01)
--no-llmanalyzeSkip AI summaries — no API key needed, faster
--no-cacheanalyzeForce full re-analysis, ignore file cache
--max-files NanalyzeLimit files analyzed
--langanalyzeFilter: python, js, ts, dart, go
--repo <path>allPath to repository (default: current dir)
--forceinitOverwrite existing CLAUDE.md
--tokenmine-prsGitHub token (or set GITHUB_TOKEN env)
--max-prs Nmine-prsMax PRs to analyze (default: 50)
--levelrisksFilter: HIGH, MEDIUM, LOW, all
--stale-onlyfreshnessShow only stale/outdated files

Claude Code Integration

There are two ways to use Cortex with Claude Code:

Option A: MCP Server (automatic)

Claude Code queries Cortex directly when it needs context. No manual steps required.

// .claude/settings.json
{
  "mcpServers": {
    "cortex": {
      "command": "cortex",
      "args": ["mcp"]
    }
  }
}

Option B: Manual context (explicit)

Add to your CLAUDE.md:

## Project Context

Before editing any file, run:
```bash
python .claude/get_context.py <file_path>
```

Claude Code will run this command and read the output before making changes.

GitHub Actions

Auto-update context on every push to keep it fresh for the whole team.

# .github/workflows/cortex.yml
name: Update Cortex Context

on:
  push:
    branches: [main, master]

jobs:
  cortex:
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
        with:
          fetch-depth: 0
      - uses: actions/setup-python@v5
        with:
          python-version: '3.11'
      - run: pip install git+https://github.com/kovalenko-tech/cortex.git
      - run: cortex analyze --no-llm
      - uses: stefanzweifel/git-auto-commit-action@v5
        with:
          commit_message: "docs: update cortex context [ci]"
          file_pattern: ".claude/**"

Add ANTHROPIC_API_KEY as a repository secret to enable AI summaries in CI.

Git Pre-commit Hook

Automatically update context for modified files before each commit.

cortex install-hook

This installs a pre-commit hook at .git/hooks/pre-commit that runs cortex analyze --since HEAD on staged files and adds the updated context to the commit.

The hook runs incrementally — only analyzes files that changed in the current commit, not the whole project.

Use Case: Solo Developer

You work on a project alone and use Claude Code regularly. The problem: every session starts fresh and you spend the first few minutes re-explaining what the project does.

Setup (one time)

cd your-project
cortex setup        # analyze + MCP config + git hook
git add .claude/ CLAUDE.md
git commit -m "add cortex context"

Daily workflow

The pre-commit hook keeps context fresh automatically. When you open Claude Code, it has full context. No extra steps.

When to run manually

cortex risks     # check before a big refactor
cortex freshness # if you haven't committed in a while
cortex deps      # before a release

Use Case: Team Workflow

Multiple developers work on the same codebase. You want everyone's Claude Code to have the same context without each person running analysis.

Setup with GitHub Actions

Set up the GitHub Action to auto-update context on every merge to main. The .claude/docs/ directory is committed to the repo — everyone gets it when they pull.

PR knowledge mining

Run cortex mine-prs periodically to capture team decisions from PR discussions. Commit .claude/pr-knowledge.json alongside the context files.

export GITHUB_TOKEN=ghp_...
cortex mine-prs
cortex analyze
git add .claude/
git commit -m "update cortex context + PR knowledge"

Use Case: Onboarding New Developers

A new developer joins the team and needs to understand the codebase. Normally this takes days of reading code and asking questions.

With Cortex

If .claude/docs/ is committed to the repo, the new developer clones it and immediately has:

  • Which parts of the codebase have a bug history
  • Which files are high-risk and should be approached carefully
  • Why certain architectural decisions were made (from PR knowledge)
  • Which files have no tests
  • Security issues to be aware of

Tip: generate CLAUDE.md first

cortex init

This generates a CLAUDE.md with project overview, key directories, build and test commands. Claude Code reads this first — useful for humans too.

Use Case: Code Review

Before reviewing a PR or making changes to a risky file, get the full context first.

Before reviewing a file

cortex context src/auth.py

Shows historical bugs, risk score, PR decisions, and security notes for that specific file.

Before merging a branch

cortex diff main # update context for changed files only
cortex risks     # see if any HIGH risk files were modified

After a large refactor

cortex analyze --no-cache # full re-analysis with updated risk scores