Categories Alternatives News Submit a Tool Advertise About
Home/ Tools/ AI Context Manager

AI Context Manager

AI Context Manager

Make your AI coding assistant "call modules like functions"
No more reverse-engineering the entire codebase

Python 3.10+ MIT Open Source Free Forever 🇨🇳 中文支持
60-90%
Token Savings
2K-5K
Tokens per Task (Contract Mode)
15K-50K
Tokens per Task (Traditional)
Zero
Context Amnesia Between Sessions

The Problem — Context Bloat

❌ Traditional — AI Scans Everything

AI reads 20+ source files to "understand" the project before making any change. Token consumption: 30K+. Every new session starts from scratch, wasting time and money.

✅ Contract Mode — AI Reads What Matters

AI reads one 500-token contract file and immediately understands the module's full API and boundaries. Token consumption: under 3K. Persistent between sessions.

How It Works

📖
Read PROJECT.md
Project map overview
📄
Read .contract.md
Module interface
🎯
Open target source
Only 1 file
Done in 2K-5K tokens
90% less waste

Contract File Example

# Module: payment
> Payment processing — orders, refunds, billing

## Public API
### Functions
async create_order(amount, currency) → Order
refund(order_id, reason, partial=False) → RefundResult

### Classes
- Ordermark_paid(), cancel()

## Dependencies
- database — order and refund persistence
- gateway  — payment gateway (Stripe, WeChat Pay)

## Side Effects
- [x] Database writes (orders, refunds)
- [x] Network requests (payment gateway API)

## Files
- src/payment/service.py — core payment logic
- src/payment/models.py — Order, RefundResult dataclasses

500 tokens. AI reads this and fully understands the payment module's capabilities — no need to read 300 lines of implementation.

Install & Use

$ pip install git+https://github.com/KaiDev-dev/ai-context.git

$ cd your-project
$ ai-context init              # English mode (default)
$ ai-context init --lang zh    # Chinese mode
$ ai-context scan              # Scan code & generate contracts
$ ai-context status            # View token savings report

Generated Project Structure

your-project/
├── .ai/                      # AI context layer (commit to Git)
│   ├── PROJECT.md            # Project overview — AI reads first
│   ├── GUIDE.md              # AI development rules
│   └── contracts/
│       ├── auth.contract.md
│       ├── payment.contract.md
│       └── database.contract.md
├── src/                      # Your code (unchanged)
└── ...

All Commands

CommandDescription
ai-context initInitialize .ai/ directory (English)
ai-context init --lang zhInitialize .ai/ directory (Chinese)
ai-context scanScan project, auto-generate all contracts
ai-context gen <name>Create contract skeleton for new module
ai-context mapUpdate PROJECT.md project map
ai-context checkVerify contracts are in sync with code
ai-context statusView token comparison report

Configure Your AI Tool

Your AI tool needs to know where to find the contracts. Add these rules to your project config.

Cursor.cursorrules

## Context Rules
Always read .ai/PROJECT.md first to understand the project structure.
Before modifying any module, read its contract at .ai/contracts/<module>.contract.md.
Use the Files section in contracts to locate source files — do not scan directories.
After changing a public API, remind the user to run `ai-context scan`.

Claude CodeCLAUDE.md

## Project Context
- Read .ai/PROJECT.md first for the module map
- Read .ai/contracts/*.contract.md for module APIs before any changes
- Contracts define the interface; source code is the implementation
- After modifying a public API, remind the user to run `ai-context scan`

Windsurf.windsurfrules

## Context Rules
Read .ai/PROJECT.md first to understand project structure.
Before modifying any module, read the corresponding .contract.md.
Use the Files field to locate source files — don't scan directories.
After changing public APIs, remind user to run ai-context scan.

GitHub Copilot.github/copilot-instructions.md

Read .ai/PROJECT.md first to understand project structure.
Before modifying any module, read the corresponding .contract.md.
Do not scan src/ directory to find files.
After changing public APIs, remind user to run ai-context scan.

Cline.clinerules

## Context Rules
Read .ai/PROJECT.md first for project map.
Read .ai/contracts/*.contract.md before module changes.
Use Files field to locate source files.
Remind to run ai-context scan after API changes.

💡 Auto-configuration is planned. For now, copy-paste the relevant block above into your tool's rules file.

Real-World Savings

On the ai-context project itself (3 Python files):

Project: ai-context

PROJECT.md: OK
GUIDE.md:   OK
Contracts:  1

Token comparison:
  Traditional (full scan): ~9,000 tokens
  Contract mode (on demand): ~548 tokens
  Savings: 94%

For a 50-file FastAPI project, estimated savings: 75%-85%.

PROJECT.md: OK GUIDE.md: OK Contract files: 1 Token comparison: Traditional (full scan): ~9,000 tokens Contract mode (scoped): ~548 tokens Savings: 94%

For a 50-file FastAPI project, estimated savings: 75%-85%.

FAQ

Do I need to manually maintain contracts?
No. ai-context scan auto-extracts APIs from your code. Re-run after API changes.
Should I commit contracts to Git?
Yes. The .ai/ directory should be committed — shared contracts ensure team-wide AI consistency.
How do I switch languages?
Run ai-context init --lang zh for Chinese, or omit --lang for English (default). The setting is saved in .ai/.contractconfig. To change later, edit that file and re-run ai-context scan.
What languages are supported?
Currently Python (AST parsing) and TypeScript/JavaScript. Go, Rust, Java planned.
How is this different from .cursorrules?
.cursorrules tells AI how to write code (conventions, style). Contracts tell AI what the code does (APIs, dependencies, effects). They complement each other.