{"name":"ctx-gate: LLM Context Gateway for Efficient Token Usage","description":"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.","github":"https://github.com/nac7/ctx-gate","url":"https://osrepos.com/repo/nac7-ctx-gate","source":"osrepos.com","sourceDescription":"This repository profile is provided by osrepos.com, an open source repository discovery platform.","repositoryProfile":"https://osrepos.com/repo/nac7-ctx-gate","generatedFor":"open source discovery and AI-assisted research","markdown":"https://osrepos.com/repo/nac7-ctx-gate.md","json":"https://osrepos.com/repo/nac7-ctx-gate.json","topics":["llm","context-management","token-optimization","ai-agents","openai","anthropic","python","developer-tools"],"keywords":["llm","context-management","token-optimization","ai-agents","openai","anthropic","python","developer-tools"],"stars":null,"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.","content":"## Introduction\n`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.\n\n## Why Use and Key Benefits\nLLM 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:\n\n*   **Task Shift Detector**: Automatically clears context when a new task is detected, carrying forward only key facts to the new session's system prompt.\n*   **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.\n*   **Model Router**: Dynamically selects the appropriate model tier (fast, standard, advanced) based on prompt complexity, optimizing both cost and performance.\n*   **Checkpoint Writer**: Saves session state for restart recovery, injecting the last checkpoint into new system prompts automatically.\n\nThe project also includes a robust faithfulness evaluation harness, which measures token savings and information retention, providing concrete evidence of its effectiveness.\n\n## Installation\nGetting started with `ctx-gate` is straightforward. You can install it via pip:\n\nbash\npip install ctx-gate\n\n\nFor development or to track the `main` branch, you can install from source:\n\nbash\ngit clone https://github.com/nac7/ctx-gate\ncd ctx-gate\npip install -e .\n\n\nOptional extras for enhanced functionality:\n\nbash\npip install tiktoken                      # for exact token counts\npip install \"ctx-gate[rag]\"               # for RAG retrieval + embedding relevance\n\n\n## Examples\nOnce installed, you can start the `ctx-gate` proxy and point your LLM tools to it.\n\n**Start the proxy:**\n\nbash\n# Claude (default)\nANTHROPIC_API_KEY=sk-ant-... python ctx_gate.py serve --verbose\n\n# OpenAI\nOPENAI_API_KEY=sk-... python ctx_gate.py serve --provider=openai\n\n# Local Ollama (no key needed)\npython ctx_gate.py serve --provider=ollama\n\n# Custom port\npython ctx_gate.py serve --port=9000\n\n\n**Point your tool at `ctx-gate`:**\n\n*   **Claude Code** (`~/.claude/settings.json`):\n    \n    {\n      \"env\": {\n        \"ANTHROPIC_BASE_URL\": \"http://127.0.0.1:8080\"\n      }\n    }\n    \n*   **Cursor / Continue.dev / VS Code**: Change the API base URL to `http://127.0.0.1:8080/v1`.\n*   **Any OpenAI SDK**:\n    python\n    from openai import OpenAI\n    client = OpenAI(base_url=\"http://127.0.0.1:8080/v1\", api_key=\"any\")\n    \n\n## Links\n*   **GitHub Repository**: <a href=\"https://github.com/nac7/ctx-gate\" target=\"_blank\">https://github.com/nac7/ctx-gate</a>\n*   **PyPI**: <a href=\"https://pypi.org/project/ctx-gate/\" target=\"_blank\">https://pypi.org/project/ctx-gate/</a>\n*   **DOI**: <a href=\"https://doi.org/10.5281/zenodo.21877377\" target=\"_blank\">https://doi.org/10.5281/zenodo.21877377</a>","metrics":{"detailViews":0,"githubClicks":0},"dates":{"published":null,"modified":"2026-09-16T11:53:28.000Z"}}