Skip to main content

Vetch

PyPI version Python versions License CI Open In Colab

Stop runaway inference.

Vetch detects stalled agents, RAG bloat, excessive generation, zombie LLM calls, context snowballs, invisible output burn, prompt cache opportunities, repeated truncation, and large-model rightsizing candidates. It turns those patterns into metadata-only advisory signals, and can warn, kill, or reroute confirmed stalled loops before they burn budget, latency, energy, and carbon.

import vetch

vetch.instrument()
vetch.set_stall_action("kill")  # or "warn", or "reroute"

with vetch.Session():
    run_agent()  # your existing agent loop
    # If it stalls (short outputs, most calls sending the same input length: the
    # signature of a stuck loop), the next LLM call raises vetch.StallDetected
    # before more money is wasted.

Capabilities

  • Providers (auto-instrumented): OpenAI, Anthropic, Google GenAI (google-genai), Google Vertex AI, Azure OpenAI, Ollama. pip install vetch[openai], vetch[anthropic], vetch[genai], vetch[vertexai], vetch[ollama].
  • Self-hosted / OpenAI-compatible: vLLM, TGI, LM Studio, and llama.cpp, reached through an OpenAI client with a custom base_url (classified automatically) or raw HTTP through vetch.proxy / vetch.wrap(). See Self-hosted and raw HTTP.
  • Streaming: input/output token instrumentation for sync and async streams, without buffering response content.
  • Framework integrations: LangChain and LlamaIndex callback handlers; first-party Vercel AI SDK middleware (JS/TS).
  • Control: circuit breakers (stall detection → warn/kill/reroute) and warn-only budgets on cost/energy/carbon.
  • Export & tooling: OpenTelemetry / OTLP export (GenAI semantic conventions), an MCP server for agents, a CLI (vetch estimate|compare|audit|calibrate), and local GPU calibration.
  • Metadata-only: never stores or sends prompt or response text. It reads the model, token counts, timing and finish reason, and counts the characters of each response to estimate usage and spot empty answers.

The problem

Old cloud waste was idle infrastructure: overprovisioned servers, forgotten instances, jobs that ran once and stayed scheduled. You could fix it by turning things off.

AI waste is different. It is active, accumulating, and invisible until the bill arrives. A stalled agent loop burns tokens on every iteration. A RAG pipeline retrieving irrelevant context bloats every prompt. A session that should have ended 40 calls ago is still running. Provider dashboards show total spend. They do not show which feature, customer, workflow, or agent session produced the waste, and they cannot stop the next occurrence automatically.

Every wasted inference call is wasted money, compute, energy, and carbon.

Why not just use your provider dashboard?

  • No attribution. Dashboards show cost by model and date, not by agent session, customer, or feature flag.
  • Read-only. They cannot fire a circuit breaker when a session exceeds a budget or an agent loop stalls.
  • No pattern detection. A dashboard cannot spot that 80% of your agent's last ten outputs were under 20 tokens, the signature of a stalled loop.
  • No per-call energy or carbon data. Reporting on inference resource use needs per-call instrumentation the provider does not expose.

Detected waste patterns

Vetch analyzes every inference call for behavioral patterns that indicate waste. Each pattern has a stable advisory ID.

Advisory Pattern Signal Status
STALL-001 Stalled agent loop ≥80% of last 20 calls produce short output with repeated input ✅ Implemented
CACHE-001 Prompt caching opportunity >50% of calls share identical input token counts across ≥6 calls ✅ Implemented
CACHE-002 Cache not active Same repetition signal as CACHE-001 but no cache reads observed ✅ Implemented
RAG-001 RAG bloat Average input:output ratio exceeds 50:1 ✅ Implemented
BABBLE-001 Excessive generation Recent average output exceeds 1,500 tokens without long-form task signal ✅ Implemented
ZOMBIE-001 Post-completion drift Repeated normal-length outputs after likely task completion ✅ Implemented
CTX-001 Context snowball The prompt gets larger every turn while useful output stays low ✅ Implemented
EMPTY-001 Invisible output burn Output tokens consumed while visible output is near-empty ✅ Implemented
TRUNC-001 Repeated response truncation Frequent finish_reason=max_tokens or length across recent calls ✅ Implemented
STREAM-001 Incomplete streams ≥30% of streaming calls cancelled before completion ✅ Implemented
REASONING-001 Reasoning model, no reasoning o1/o3 calls return no reasoning tokens ✅ Implemented
ERROR-001 Error storm ≥3 consecutive errors or ≥40% error rate in recent window ✅ Implemented
PREMIUM-001 Large model rightsizing candidate Stable tagged workflow mostly uses a premium model with cheaper eval candidates ✅ Implemented (audit-only)
TOOL-DEAD-001 Dead function tools Tools offered on many requests but never invoked ✅ Implemented
CAP-001 Declared capabilities silent Expected kind:name routes never fired in audit window ✅ Implemented (audit-only)
SESSION-BUDGET-001 Session over budget Configured cost/energy/carbon threshold exceeded ⚠️ Partial (alerts only)
ATTRIBUTION-001 Unattributed spend Required tags missing from calls ⚠️ Partial (infrastructure only)
RETRY-001 Retry storm Burst of repeated failed or near-identical calls 🔜 Planned

Full taxonomy with detection signals, false positives, and recommended actions: docs/inference-waste-taxonomy.md.

Advisories are deterministic signals, not proof of waste. Confidence labels indicate signal strength from metadata patterns, not statistical certainty. Non-stall runtime advisories are warn-only; PREMIUM-001 is audit-only and queues eval candidates rather than recommending an automatic downgrade. Automatic kill and reroute are scoped to STALL-001.

Quick start

Two lines to start tracking inference waste in your existing LLM calls. See QUICKSTART.md for the complete 60-second guide.

instrument(): global, zero-touch

One line at startup. Every LLM call across all providers is tracked automatically:

import vetch
import openai

vetch.instrument(region="us-east-1", tags={"service": "chat-api"})

client = openai.OpenAI()
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "Hello world"}]
)
# Cost, energy, carbon, and advisory events emitted automatically

wrap(): per-call, explicit

Context manager around individual calls. Best for per-call metrics, different tags per call, or recording only some calls. It works without instrument():

from vetch import wrap

with wrap(region="us-east-1", tags={"team": "ml", "env": "prod"}) as ctx:
    response = client.chat.completions.create(
        model="gpt-4o",
        messages=[{"role": "user", "content": "Hello world"}]
    )

print(f"Cost:   ${ctx.event['estimated_cost_usd']}")
print(f"Energy: {ctx.event['estimated_energy_wh']} Wh")
print(f"Carbon: {ctx.event['estimated_carbon_g']} gCO2e")

When to use which:

instrument() wrap()
Setup One line at startup Context manager around one or more calls
Scope All calls, all providers The calls inside the block, one event each
Tags Same tags for everything Different tags per call
Metrics access Via event callbacks ctx.event (last call), ctx.total_cost_usd and friends (whole block)
Best for Production services Notebooks, experiments, per-feature attribution

Both are fail-open and add <5ms overhead. Async is supported via awrap().

Order doesn't matter. Both cover clients you have already created and SDKs you import later. Check coverage at runtime with vetch.instrumentation_status(). Vetch is tested with openai 1.x to 3.x, anthropic 0.18 to 2.x and google-genai up to 3.x. Calls through a newer version are still recorded, with a warning.

Attribute waste

Every inference call is tagged and attributed to a session. Sessions can carry any tags you define (feature, customer, user, workflow, environment, team), and cost, energy, and carbon accumulate per session and per tag combination.

import vetch

with vetch.Session(tags={"agent": "researcher", "task": "summarize"}) as session:
    with vetch.wrap(tags={"feature": "rag-search", "customer": "acme"}) as ctx:
        response = client.chat.completions.create(...)

print(f"Total cost:   ${session.total_cost_usd}")
print(f"Total energy: {session.total_energy_wh} Wh")
print(f"Call count:   {session.call_count}")
print(f"Not in the cost: {session.unpriced_calls} calls to models with no known price")

Vetch never guesses a price. If a model is missing from the registry, estimated_cost_usd is None, the energy estimate is marked low confidence, and sessions, budgets, and reports count the call as unpriced.

Sessions nest, and support distributed propagation across microservices via session.inject_headers() / vetch.Session.from_headers(). See docs/attribution.md for tool/capability observability (dead-tool schema waste, configure_capabilities) and header propagation examples.

Stop waste automatically

When STALL-001 fires, Vetch can intervene without manual action:

Action What happens
"log" (default) Generate the advisory, take no action. Backwards compatible.
"warn" Log a stderr warning on the next call after a stall.
"kill" Raise vetch.StallDetected on the next call, breaking the loop.
"reroute" Transparently substitute the model with fallback_model.

set_stall_action is currently wired to STALL-001. Per-advisory, per-tag, and per-session policies are planned; see ROADMAP.md.

vetch.StallDetected inherits from RuntimeError, so except ValueError: will not swallow it, but except Exception: will. Every further call is refused before it reaches the provider, so nothing more is spent, but a loop that retries on except Exception: keeps spinning. Put except vetch.VetchInterrupt: raise before it. Recover with session.clear_stall() after a human-in-the-loop fix. The circuit breaker needs an explicit Session. docs/how-detection-works.md explains why instrument() alone is observability-only.

Thresholds can be tuned per workflow when a pattern is expected. For example, a classification route that returns three tokens can lower the STALL-001 low-output threshold without affecting other routes:

with vetch.Session(
    tags={"route": "classifier"},
    advisory_thresholds={"STALL-001": {"low_output_threshold": 1}},
):
    response = client.chat.completions.create(...)

Prove savings: the 7-day audit

vetch audit reads locally stored metadata, runs advisory detection, computes per-tag attribution, and estimates observed and projected avoidable cost. A typical adoption motion:

  1. Instrument and store. Call vetch.instrument(region="us-east-1", tags={"service": "my-service"}), then vetch.configure_storage(). The audit reads what the second call keeps in ~/.vetch/usage.db: metadata only, and off unless you ask for it.
  2. Tag and observe. Add tags to attribute spend, and run in set_stall_action("warn") to watch advisories without intervening.
  3. Audit. After real traffic accumulates, run the report.
  4. Promote. For confirmed STALL-001 patterns, move to "kill" or "reroute". Treat non-stall advisories as a review queue: fix the workflow, retriever, cache config, or attribution gaps before automating.
vetch audit                    # last 7 days (default)
vetch audit --window 24h       # shorter window
vetch audit --tags team=ml     # filter by tag
vetch audit --format json      # machine-readable
vetch audit --format markdown  # shareable report

Output includes advisory findings with signal-strength labels and recommended actions, per-tag attribution breakdowns, observed and projected avoidable cost, and data-quality indicators (tagged fraction, methodology versions). Full report format and adoption walkthrough: docs/audit-report.md.

Programmatic access is available via vetch.audit_report.build_audit_report() / format_audit_report().

Energy and carbon

Vetch treats energy and carbon as first-class outputs alongside cost. Figures should be read with explicit uncertainty, because not all numbers are equal. Every event carries energy_tier, energy_uncertainty_pct, and model_match so you can tell measured from inferred from guessed.

Tier Name Uncertainty Source
0 Measured ±10–20% Direct GPU telemetry from a local vetch calibrate run
1 Inferred ±20–50% Infrastructure-aware benchmarking of hosted APIs (no power meter exists)
2 Validated ±50–100% Crowdsourced aggregates
3 Estimated Order of magnitude Parameter-based calculation, or a proxy/family match

The bundled registry lists each model it covers, with aliases resolving many more dated and versioned names. Run vetch methodology for the current per-model tier and provenance. Use these estimates as internal inputs for FinOps, engineering, and sustainability planning. They are not carbon certification, regulatory disclosure, or water accounting. For methodology, citations, and Tier 1 coverage, see docs/energy-methodology.md.

Model coverage and resolution

Vetch resolves a model name to a registry entry through a ladder; each event records which rung matched in model_match: exact → alias → prefix (downgraded to Tier 3) → family (Tier 3) → fallback. If a current model isn't in the bundled registry yet, add a row (see src/vetch/registry/PROVENANCE.md), vetch calibrate it, or pass energy_override to wrap(). Details: docs/model-resolution.md.

Region configuration

The region parameter selects the electricity grid used for carbon intensity, and should match the Electricity Maps zone identifier (typically aligned with cloud region names: us-east-1, eu-west-1, eastus). OpenAI and Anthropic route globally and do not expose per-call location, so use your best estimate.

If region is unset, Vetch falls back to VETCH_REGION, then cloud provider env vars, then a coarse timezone heuristic (often inaccurate). Always set region or VETCH_REGION explicitly for accurate carbon numbers. See docs/region-config.md.

Budget alerts

import vetch

vetch.set_budget("hourly", cost_usd=10.0, energy_wh=50.0, window="hour")

@vetch.on_budget_alert
def handle_alert(alert):
    print(f"Budget alert: {alert}")

window can be "request" (the default), "session", "hour" or "day". Budget thresholds never block LLM calls; they trigger alerts only. Blocking policies are planned.

OTLP export (Grafana, Datadog)

Export waste advisories, per-call cost, energy, and carbon to any OpenTelemetry-compatible backend:

import vetch

vetch.configure_otlp_export(endpoint="http://localhost:4317", service_name="my-llm-service")
# vetch dashboard --export grafana --output grafana_vetch.json   # pre-built dashboard

MCP server (AI agent integration)

Vetch ships an MCP server (pip install vetch[mcp]) that gives agents real-time access to energy, cost, and carbon data, so they can check budgets, compare models, and make sustainability-aware decisions mid-conversation.

{
  "mcpServers": {
    "vetch": {
      "command": "vetch-mcp",
      "env": { "VETCH_REGION": "us-east-1" }
    }
  }
}

Tools include vetch_estimate, vetch_compare, vetch_session_stats, vetch_status, vetch_check_budget, vetch_grid_intensity, vetch_cleanest_region, and vetch_registry_lookup. Full tool and resource reference: docs/mcp.md.

CLI usage

vetch status                                                  # status and configuration
vetch estimate --model gpt-4o --input-tokens 1000 --output-tokens 500
vetch compare --models gpt-4o,claude-3-opus,gemini-1.5-pro --input-tokens 1000
vetch audit --window 24h --tags team=ml --format json         # stored-event audit
vetch report --days 7 --tags team=ml                          # usage report
vetch dashboard --export grafana --output dashboard.json
vetch registry freeze --output vetch_registry.json            # freeze for CI/CD

Self-hosted and raw HTTP

Self-hosted serving (vLLM, TGI, LM Studio, llama.cpp) is usually reached through the OpenAI SDK with a custom base_url, or via raw HTTP. Vetch handles both, and never bills a self-hosted or third-party endpoint at OpenAI's per-token rates:

import vetch
from openai import OpenAI

vetch.instrument()
client = OpenAI(base_url="http://localhost:8000/v1", api_key="x")  # local vLLM
#   api.openai.com / *.openai.azure.com  -> "openai"             (OpenAI energy + list price)
#   localhost / 127.0.0.1 / private IPs  -> "self-hosted"        (calibration energy, cost = 0)
#   other public hosts (OpenRouter, ...) -> "openai-compatible"  (registry energy, cost = unknown)

If a private host can't be auto-detected, set VETCH_SELF_HOSTED=true. Raw HTTP requests bypass every SDK patch, so meter them explicitly with record_usage() from the token counts the response already carries:

import vetch, httpx

resp = httpx.post("http://localhost:8000/v1/chat/completions", json=body).json()  # body = your request payload
usage = resp["usage"]
vetch.record_usage(
    model="llama-3-70b",
    input_tokens=usage["prompt_tokens"],
    output_tokens=usage["completion_tokens"],
    provider_hint="self-hosted",  # cost 0, energy/carbon still computed
    region="us-east-1",
)

record_usage runs the same calculation and emit path as an instrumented call, so the event is schema-identical and flows into the same sessions, budgets, and exporters. Full examples and vetch.proxy usage: QUICKSTART-LOCAL.md.

GPU calibration (local inference)

For local inference, calibrate energy measurements against actual GPU power draw for a Tier 0 figure:

from vetch.calibrate import calibrate_model, format_calibration_result

def my_inference():
    response = ollama.generate(model="llama3.1:8b", prompt="Hello world")
    return 100, 50  # (input_tokens, output_tokens)

result = calibrate_model("ollama", "llama3.1:8b", workload=my_inference)
print(format_calibration_result(result))

Requirements: NVIDIA GPU with pynvml (pip install 'vetch[calibrate]'). On Apple Silicon, use vetch calibrate-apple-silicon (powermetrics-based, requires sudo). Both paths write a schema-2 record under ~/.vetch/calibrations/ (picked up automatically). vetch validate-calibration file.json checks identifiability on a laptop with no GPU. See docs/calibration-protocol-v2.md. To share a calibration, open a GitHub issue with that JSON (see the CLI result footer). See QUICKSTART-LOCAL.md.

Supported providers

Provider Status Instrumentation
OpenAI Supported vetch.instrument() or vetch.wrap()
Azure OpenAI Supported vetch.instrument() (auto-detects AzureOpenAI)
Anthropic Supported vetch.instrument() or vetch.wrap()
Vertex AI (Gemini) Supported vetch.instrument() or vetch.wrap()
Ollama Supported Native SDK or OpenAI-compat API (auto-detected)
OpenRouter / Together.ai / Anyscale Compatible OpenAI-compatible API
vLLM / TGI Compatible OpenAI-compatible API

OpenAI-compatible endpoints work automatically with vetch.instrument() and are classified by base_url so they are not billed at OpenAI's rates. See Self-hosted and raw HTTP.

Environment variables

Variable Description
VETCH_DISABLED Set to true to completely disable Vetch (emergency kill switch)
VETCH_REGION Default grid region (e.g., us-east-1, eu-west-1)
VETCH_OUTPUT Output target: none (default), stderr, a file path, or logging (events go to your app's own logging setup, at INFO on the vetch.emitter logger)
VETCH_THREAD_CONTEXT Set to false to stop work submitted to a ThreadPoolExecutor inside a Session or wrap() from belonging to it
VETCH_HOME Vetch home directory (default: ~/.vetch/)
VETCH_REGISTRY_REMOTE Set to false to disable remote registry updates
VETCH_REGISTRY_PATH Path to offline registry directory (air-gapped environments)
VETCH_REGISTRY_URL Custom remote registry URL
VETCH_SELF_HOSTED Force the self-hosted provider label when the host can't be auto-classified
ELECTRICITY_MAPS_API_KEY API key for live grid carbon intensity data
VETCH_CACHE_MODE Set to memory-only for serverless/Lambda environments
VETCH_CI_SUMMARY Set to 1 to print a summary of the process's calls (count, energy, carbon, cost) to stdout when it exits. Experimental. Off by default, in CI too

Design guarantees

  • Fail-open. Every operation (patching, calculation, emission) is wrapped in isolated error handlers. If Vetch fails, your LLM call proceeds normally and a tracking_disabled: true event is logged. Vetch will never cause an inference outage.
  • Privacy. Vetch never stores prompt or completion text. It records only token counts, model names, timing, tags, finish reason, and visible output character count. No PII or prompt data leaves your environment.
  • Thread safety. contextvars for async session isolation, locked session stats, and WeakKeyDictionary for client patching. Work submitted to a ThreadPoolExecutor inside a Session or wrap() belongs to it, as with asyncio.to_thread (Vetch makes submit copy the caller's context while one is open; VETCH_THREAD_CONTEXT=false turns this off); a plain threading.Thread does not. Create a Session per request/job/agent; set global config (set_stall_action, set_advisory_thresholds) at startup, not per request.

Current limitations

  1. Energy estimates are uncertain. Most models use Tier 3 estimates. See vetch methodology.
  2. Region inference is a coarse heuristic. Set region or VETCH_REGION explicitly for accurate carbon numbers.
  3. Automatic intervention is wired to STALL-001 only. Per-advisory/tag/session policies are planned.
  4. Experimental modules. vetch.calibrate, vetch.storage, and vetch.ci emit FutureWarning and may change.

Troubleshooting

export VETCH_DISABLED=true   # emergency kill switch
export VETCH_OUTPUT=none     # silence all output
import logging
logging.getLogger("vetch").setLevel(logging.DEBUG)  # debug logging

Contributing

See CONTRIBUTING.md for development setup, testing guidelines, and how to contribute energy data.

License

Apache License 2.0. See LICENSE and NOTICE for details.

Metadata

Release files for vetch 0.13.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for vetch 0.13.1
File Size Uploaded Provenance Actions
vetch-0.13.1.tar.gz 709.6 kB With provenance Details for vetch-0.13.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for vetch 0.13.1
File Interpreter ABI Platform Actions
vetch-0.13.1-py3-none-any.whl Python 3 none any Details for vetch-0.13.1-py3-none-any.whl

Total release size: 1.2 MB

Release files / vetch-0.13.1.tar.gz

Download URL vetch-0.13.1.tar.gz
Size 709.6 kB
Tags Source
SHA-256 checksum
How to use checksums
22901dad5875b73b5d59a291b0d390b6123c5528edb28a1e89f9e26ef134e8dd
BLAKE2b-256 checksum
How to use checksums
b81d7e8aaafc9351909703e25e7ba489f9c71d05679a80c81fc290ffdbe88570
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Release files / vetch-0.13.1-py3-none-any.whl

Download URL vetch-0.13.1-py3-none-any.whl
Size 459.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9c1b5da4c0d91b4f3d1e8e6933b73a3051e6117f79966e88f0c09622f8563640
BLAKE2b-256 checksum
How to use checksums
57ae7f4864ed53685ea75bf140efc9bdb39ad383121af70adf497085056438ba
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.14

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

Security for vetch 0.13.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for vetch-0.13.1.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 6, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.13.1 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page