ctx-gate: LLM Context Gateway for Efficient Token Usage
This repository profile is provided by osrepos.com, an open source repository discovery platform.

Summary
ctx-gate is an LLM-agnostic context optimization proxy that reduces token consumption in AI interactions. It intelligently prunes conversation history and tool outputs, ensuring critical facts are retained without altering your workflow. Compatible with Anthropic and OpenAI APIs, ctx-gate helps developers manage LLM costs and maintain prompt fidelity.
Repository Information
Topics
Click on any tag to explore related repositories
Use at your own risk
OSRepos shares public repositories for knowledge and discovery only. Any installation, execution, configuration, or use of code from these repositories is the user's own responsibility. Always review the repository, source code, dependencies, licenses, and security implications before running or installing anything. OSRepos is not responsible for issues, damages, or losses resulting from third-party repositories.
Introduction
ctx-gate is an innovative LLM-agnostic context optimization proxy that sits between your development environment or tool and any Large Language Model. Its primary goal is to automatically reduce session token consumption without sacrificing the essential facts your prompts rely on, all while requiring no changes to your existing workflow. This powerful gateway supports both Anthropic Messages API and OpenAI-compatible endpoints, making it versatile for various LLM-powered applications like Claude Code, Cursor, Continue.dev, and any OpenAI SDK.
Why Use and Key Benefits
LLM coding tools often consume tokens rapidly due to several factors, including compounding context, verbose tool outputs, full file reinjection, task bleed, and model overkill. ctx-gate addresses these challenges transparently at the proxy layer through its modular architecture:
- Task Shift Detector: Automatically clears context when a new task is detected, carrying forward only key facts to the new session's system prompt.
- Context Compressor: Applies various strategies, such as rolling summaries of old turns, relevance-scored retention of critical facts, file diff injection, and tool output truncation. This module ensures significant token savings while maintaining 100% fact retention, as verified by its built-in faithfulness harness.
- Model Router: Dynamically selects the appropriate model tier (fast, standard, advanced) based on prompt complexity, optimizing both cost and performance.
- Checkpoint Writer: Saves session state for restart recovery, injecting the last checkpoint into new system prompts automatically.
The project also includes a robust faithfulness evaluation harness, which measures token savings and information retention, providing concrete evidence of its effectiveness.
Installation
Getting started with ctx-gate is straightforward. You can install it via pip:
pip install ctx-gate
For development or to track the main branch, you can install from source:
git clone https://github.com/nac7/ctx-gate
cd ctx-gate
pip install -e .
Optional extras for enhanced functionality:
pip install tiktoken # for exact token counts
pip install "ctx-gate[rag]" # for RAG retrieval + embedding relevance
Examples
Once installed, you can start the ctx-gate proxy and point your LLM tools to it.
Start the proxy:
# Claude (default)
ANTHROPIC_API_KEY=sk-ant-... python ctx_gate.py serve --verbose
# OpenAI
OPENAI_API_KEY=sk-... python ctx_gate.py serve --provider=openai
# Local Ollama (no key needed)
python ctx_gate.py serve --provider=ollama
# Custom port
python ctx_gate.py serve --port=9000
Point your tool at ctx-gate:
- Claude Code (
~/.claude/settings.json):{ "env": { "ANTHROPIC_BASE_URL": "http://127.0.0.1:8080" } } - Cursor / Continue.dev / VS Code: Change the API base URL to
http://127.0.0.1:8080/v1. - Any OpenAI SDK:
from openai import OpenAI client = OpenAI(base_url="http://127.0.0.1:8080/v1", api_key="any")
Links
- GitHub Repository: https://github.com/nac7/ctx-gate
- PyPI: https://pypi.org/project/ctx-gate/
- DOI: https://doi.org/10.5281/zenodo.21877377
Related repositories
Similar repositories that may be relevant next.
Qovira: A Private, Self-Hostable AI Personal Assistant in Go
September 15, 2026
Qovira is an ambitious project aiming to deliver a private, self-hostable AI personal assistant. Built with Go and SvelteKit, it will organize reminders, notes, and schedules using AI, all on infrastructure you control. Currently in early development, Qovira promises a secure and private alternative to cloud-based AI tools.

Goon: Autonomous AI Worker for Software Development and Custom Workflows
September 15, 2026
Goon is a self-hosted, autonomous AI worker designed to streamline software development and automate custom workflows. Built with Go and having zero dependencies, it operates as a daemon, capable of tasks from writing code and opening PRs to summarizing emails and monitoring logs. It learns from your context and asks for human approval before acting, ensuring controlled and intelligent automation.

Awesome-Self-Improving-Agents: A Curated List for Agentic AI Self-Improvement
September 14, 2026
Awesome-Self-Improving-Agents is a comprehensive GitHub repository featuring a curated and continuously updated list of resources on self-improvement in foundation model-based agentic systems. It serves as a central hub for researchers and practitioners, offering papers, benchmarks, and various media. This resource is essential for anyone exploring the cutting edge of self-evolving AI agents.
Ferret MCP: AI-Powered Knowledge Extraction for Any Codebase
September 14, 2026
Ferret MCP is an MCP server designed to extract comprehensive knowledge from any codebase, combining static analysis with AI-powered deep interpretation. It provides detailed insights into architecture, patterns, dependencies, and API surface, delivering a senior engineer's analysis in seconds. This tool integrates seamlessly with various MCP clients, offering both free static analysis and advanced AI-driven reports.
Source repository
Open the original repository on GitHub.