AI Context Manager
AI Context Manager
Make your AI coding assistant "call modules like functions"
No more reverse-engineering the entire codebase
The Problem — Context Bloat
痛点——上下文膨胀
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.
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
工作原理
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 - Order — mark_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
所有命令
| Command | Description |
|---|---|
ai-context init | Initialize .ai/ directory (English) |
ai-context init --lang zh | Initialize .ai/ directory (Chinese) |
ai-context scan | Scan project, auto-generate all contracts |
ai-context gen <name> | Create contract skeleton for new module |
ai-context map | Update PROJECT.md project map |
ai-context check | Verify contracts are in sync with code |
ai-context status | View token comparison report |
Configure Your AI Tool
配置你的 AI 工具
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 Code — CLAUDE.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%.
For a 50-file FastAPI project, estimated savings: 75%-85%.
FAQ
常见问题
- Do I need to manually maintain contracts?
- No.
ai-context scanauto-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 zhfor Chinese, or omit--langfor English (default). The setting is saved in.ai/.contractconfig. To change later, edit that file and re-runai-context scan. - What languages are supported?
- Currently Python (AST parsing) and TypeScript/JavaScript. Go, Rust, Java planned.
- How is this different from .cursorrules?
.cursorrulestells AI how to write code (conventions, style). Contracts tell AI what the code does (APIs, dependencies, effects). They complement each other.