Token Guard

    1

    Local token estimation, context deduplication, and budget circuit-breaking for agent workflows.

    Free

    Security scanned

    Works with the AI Agents you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+21 more

    token-guard

    Example session with this skill installed

    Trim filler from my dev-prompt.md and check if it fits a 500 token budget for claude-3-5-sonnet.

    • Read your context and instructions
    • Compiled the token-guard

    I have processed the file through the token-guard pipeline. The trimming rules removed redundant filler and duplicate paragraphs, resulting in a 17.5% reduction in context size.

    Token Guard Audit Report

    MetricValue
    Original Tokens554
    Trimmed Tokens457
    Tokens Saved97 (17.5%)
    Active Rulesline, block, restatement
    Budget Limit500 tokens
    StatusOK_WITHIN_BUDGET
    Modelclaude-3-5-sonnet
    Estimated Cost$0.001371 (Assumed)

    Analysis

    • The block rule successfully identified a repeated instruction paragraph of 48 characters.
    • Code blocks were preserved in their entirety.
    • The request is safe to send to the provider as it falls 43 tokens under your maximum threshold.

    Next steps

    • Run estimate.py session add to record this transaction in your local receipt log.
    • Use estimate.py tools trim if you plan to include MCP tools in this request.
    • Review the pricing.yaml file to ensure the $3.00/1M input token rate matches your current tier.

    Connects securely to your tools. The creator never sees your data.

    About this skill

    Stop overpaying for agent sessions and runaway subagent loops. This local utility provides precise token estimation, context compression, and budget enforcement without calling external APIs. It addresses the high cost of long-context prompts by aggressively trimming filler, deduplicating restatements, and pruning unused tool catalogs.

    What it does

    • Token estimation calculates counts using the cl100k_base tokenizer for accurate pre-flight planning.
    • Context trimming applies five distinct rules to remove filler, duplicate lines, and redundant sentences while protecting code blocks.
    • Budget enforcement acts as a circuit-breaker to stop requests that exceed user-defined token or cost limits.
    • Tool pruning filters MCP catalogs to include only the functions required for the specific task.
    • Loop guarding monitors session logs to terminate infinite agent loops based on depth, tokens, or repetition.
    • Cost reporting generates session receipts using verified pricing data for Anthropic, OpenAI, and Google models.

    How it works

    1. Estimate the token count of a file or prompt string to establish a baseline.
    2. Trim the context using the trim command to reduce token weight by up to 18% through deduplication.
    3. Set a budget using the --max flag to gate execution and prevent high-cost API calls.
    4. Log sessions to a JSONL receipt file to track cumulative spend across multiple agent turns.

    Frameworks & tools

    Built with Python and tiktoken. Uses a local pricing.yaml configuration for model rates and cl100k_base for BPE tokenization.

    Why this beats prompting it yourself

    LLMs are poor at estimating their own token usage and often fail to prune their own context effectively. This tool provides deterministic, rule-based reduction that guarantees code integrity while stripping proven filler that agents typically ignore.

    Use cases

    • Preventing runaway costs in autonomous multi-agent workflows.
    • Shrinking massive tool catalogs before sending them to expensive long-context models.
    • Auditing team-wide API spend across different providers using local logs.
    • Optimizing session resumes by identifying stable prefixes for prompt caching.

    Known limitations

    Does not catch semantic restatements using synonyms or restatements split across hard-wrapped lines. Token counts for Claude models are estimates as they use different internal tokenizers.

    Synonym-based restatements and hard-wrapped duplicates are not caught. Model prices are a point-in-time snapshot and require manual config updates. Batch+cache pricing is an approximation.

    How to use

    1. 1

      Connect your agent to the Agensi MCP

      Copy the prompt and paste it into Claude, ChatGPT, Cursor or any agent that supports MCP. It sets up the connection for you. Manual setup

    2. 2

      Sign in to Agensi

      Your agent shows a sign-in link. Sign in and click Allow.

    3. 3

      Ask for what you need

      Your agent loads this skill and uses it on your task. It always asks before any payment.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill goes through the trust checks below.

    Security scanned

    Checked by our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    Free forever

    No account required to browse

    Trust & safety

    Security scan

    Security scan passed 6 days ago

    • Free with an Agensi account

    Requires

    Python

    Permissions

    Terminal / Shell
    Network Access
    Read Files
    Write Files
    Listed6 days ago

    What's inside

    Frequently Asked Questions

    Token Guard provides a local circuit-breaker that kills runaway agent loops, aggressively prunes redundant context/tool catalogs, and provides deterministic token estimation to prevent surprise bills from API providers.

    The tool uses the `cl100k_base` tokenizer (standard for GPT-4) and a local pricing YAML; while it provides high-accuracy estimates for most models, Claude-specific counts are approximations due to proprietary tokenizer differences.

    No, Token Guard is a local Python utility that processes your data and enforces budgets on your machine or server before any data is sent to the model provider.

    The trimming engine uses five distinct rules to strip filler and duplicates while explicitly protecting code blocks, ensuring that logic remains intact even when the surrounding prose is compressed.

    Yes, you can define specific depth, token, or repetition limits that act as a hard stop, preventing autonomous agents from entering infinite, costly loops.

    The purchase includes the core Python utility, the `pricing.yaml` configuration for model rates, and the integration documentation for adding the guard to your existing agent workflows.

    New in LLM Ops