# context slice

> ContextSlice: AST-based code pre-compiler that trims code context to save tokens for AI models. Use to avoid context overflow and minimize token usage by 80-90%.

context slice is a free AI agent skill on Agensi by 左岸奶茶, in Prompt & Skill Engineering. It loads into your AI agent through the Agensi MCP server, always in its latest approved version.

- Price: Free
- Creator: [左岸奶茶](https://www.agensi.io/creators/grundy40)
- Category: [Prompt & Skill Engineering](https://www.agensi.io/skills/prompt-skill-engineering)
- Version: 1.6.3
- Last updated: 2026-12-05
- Security: scan passed (2026-12-05); every new listing is also reviewed by a person
- Listing: https://www.agensi.io/skills/context-slice
- Tags: prompt-engineering, code-analysis, ai-context, token-optimization, ast, multi-language, token

## What it does

- Shrinks code size by over 80 percent while keeping the logic clear for the AI agent
- Map connections between files so the agent understands the whole project structure
- Supports many languages like Python, JavaScript, Java, C#, and Ruby
- Works locally on your computer to keep your private code off external servers
- Helps avoid memory errors where the AI agent loses track of long conversations

## Who it's for

- **Developers working on large projects:** Send long files to your AI without hitting memory limits or wasting money. Try: "Trim my login.ts file so I can fix the auth bug without hitting the token limit."
- **Technical founders managing costs:** Slash your AI bill by removing 80% of unnecessary code from your requests. Try: "Shrink my Django project code so I can ask the AI for a feature review cheaply."
- **Freelancers auditing unfamiliar code:** Get a clear map of how code files connect without reading every single line. Try: "Show me how the files in this React project talk to each other."

## About this skill

### ContextSlice - AI Token 省錢神器

  這是一個「免費」的開源工具，幫助你用更少的 tokens 分析程式碼，避免 AI 對話中出現「上下文溢位」問題。

  ### 🎯 為什麼需要這個工具？

  當你請 AI 幫你改程式時，常常會遇到：

  1. **一次貼太多檔案，Token 花費爆表**：改一個功能往往牽涉好幾個檔案
  2. **單一檔案太長，貼不進對話框**：幾千行的長檔案無法完美貼上
  3. **傳統 AI 工具說明書太肥**：AI 每輪對話都要先消耗大量 tokens

  ### ✨ ContextSlice 的做法：

  - 在瀏覽器或本機幫程式碼畫出關聯圖
  - 把「要改的重點」留完整，只把「相關的程式」留標題與參數外框
  - 其餘「無關程式」自動略過，避免不必要的 Token 消耗

  ### 🚀 使用方式：

  **方法 1：一鍵啟動（建議）**
  ```bash
  # 進到你的專案資料夾
  node start.mjs

  方法 2：手動安裝 CLI 工具
  # 下載這支腳本到你的工具資料夾
  node tools/ctxslice-mcp.mjs "login" --budget 4000 --hops 1

  方法 3：用 MCP 服務（與 Claude Code / OpenCode / Pi Agent 整合）
  claude mcp add --scope user context-slice -- node tools/ctxslice-mcp.mjs

  🌐 支援的程式語言：

  TypeScript/JavaScript、Python、Go、C/C++、Swift、PHP、HTML/Vue/Svelte、CSS/SCSS、Rust、Java/Kotlin、C#、Ruby、
  SQL

  📊 實測效果（參考）：

  ┌────────────────────┬───────────────┬──────────────┬────────┐
  │        專案        │   原始大小    │    精簡後    │ 節省率 │
  ├────────────────────┼───────────────┼──────────────┼────────┤
  │ 中型 Node.js 專案  │ 48,000 tokens │ 6,200 tokens │ 87%    │
  ├────────────────────┼───────────────┼──────────────┼────────┤
  │ 前端 React 專案    │ 32,000 tokens │ 4,100 tokens │ 87%    │
  ├────────────────────┼───────────────┼──────────────┼────────┤
  │ Python Django 專案 │ 38,000 tokens │ 5,300 tokens │ 86%    │
  └────────────────────┴───────────────┴──────────────┴────────┘

  💡 常見使用場景：

  - Claude Code / Copilot / Cursor 中的自動上下文管理
  - ChatGPT / 網頁版 AI 複製貼上前的預處理
  - GitHub PR Review 前的程式碼摘要
  - 技術文件產出 前的結構梳理

  🔐 隱私保護：

  所有程式碼解析、依賴建圖、基準測試與精簡封包產生皆在您的電腦本機記憶體內完成，不會將您的原始碼傳送至外部伺服器
  。

## Requires

- Node.js v20+ (local installation required)

## Known limitations

Requires Node.js runtime. Pruning effectiveness depends on accurate AST parsing of the specific language syntax.

## Technical details

Built as an AST-based code pre-compiler. Uses a Node.js script (start.mjs or ctxslice-mcp.mjs) to parse code into an abstract syntax tree and prune non-essential nodes based on a specified token budget and 'hops' (dependency distance). Supports MCP integration for Claude Code and similar agents. Processes data in local memory to ensure privacy.

## How to use this skill

1. Connect your AI agent to Agensi once: add the MCP server https://mcp.agensi.io/mcp/auth and sign in with your Agensi account (OAuth, no API key). Steps for Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and other agents: https://www.agensi.io/docs/mcp-setup
2. Ask your agent: "Use the context slice skill from Agensi" and describe your task.
3. Your agent loads the skill from Agensi when it needs it. Nothing is downloaded.

## Frequently asked questions

### How does Context Slice reduce token usage without losing the meaning of my code?

Context Slice is an AST-based pre-compiler that analyzes your codebase to strip away boilerplate and irrelevant implementation details, leaving only the essential logic and signatures needed for an LLM to understand your project structure without hitting token limits.

### Which AI agents and LLM platforms are compatible with this skill?

This skill is optimized for CLI-based tools like Claude Code and OpenCode, but it also supports Pi Agent and can be used to pre-process code blocks for any web-based model like ChatGPT.

### Does my source code stay secure and local during the pruning process?

Yes, Context Slice runs entirely in your local memory to map dependencies and prune logic, ensuring your proprietary source code is never uploaded to an external server during the slicing process.

### Can I customize the output size to fit specific model limits like GPT-4o or Claude 3.5 Sonnet?

The tool features a built-in budget enforcement flag (e.g., --budget) that allows you to set a hard token limit, ensuring the output fits perfectly within your specific model's context window.

### Where I can find the latest version ?

https://github.com/grundy40/ContextSlice

---

Source: [Agensi](https://www.agensi.io), the marketplace of skills for AI agents. Any MCP-compatible agent (Claude, ChatGPT, Claude Code, Codex, Cursor, VS Code and more) connects at https://mcp.agensi.io/mcp/auth and signs in with an Agensi account; setup for every agent: https://www.agensi.io/docs/mcp-setup. Append .md to any skill, guide or docs address on agensi.io for its markdown version.
