Skip to main content

cap-shield

Context selection for AI agents. Measured, not estimated.

pip install cap-shield
from cap_shield import CapShield

cap = CapShield(api_key="cap_live_...")

cap.remember("support", "Customer reports a delayed parcel, order 4471")
ctx = cap.context("support", "what did the customer complain about?")

print(ctx.text)              # send this to your model
print(ctx.saving_pct, "%")   # how much you did not send
print(ctx.left_behind)       # ...and how many entries were left behind

What this does

Your agent's history grows every turn, and the whole thing is billed on every call. This selects what answers the question and leaves the rest.

Measured on our corpus with cl100k_base: 80.5 % of context not sent at a 1000-token budget. Measured on LongMemEval-S, 500 questions (2026-09-28): 97.1 % retrieval recall against 51.9 % for word matching.

That second number is the one that matters. A saving is worthless if what was dropped is what your agent needed — so we publish both. Current figures: https://cap-shield-robin.fly.dev/.well-known/cap-shield.json

Answers from memory, with your own model

For questions the narrow selection cannot answer (dates, counts across sessions, details that ranked low), escalation_context() fetches a wider, date-headed context. The escalation helper shipped in the same package answers it with your model key, from your machine. cap_shield itself still never calls a model and never sees the key.

from cap_escalate_helper import eskalera_om_behovs_v6

fraga = "Which bike did I fix last weekend?"
ctx = cap.escalation_context("support", fraga)
svar = eskalera_om_behovs_v6(ctx, fraga, modell="openai",
                             modell_namn="gpt-6-luna",
                             dagens_datum="2026/11/16")  # OPENAI_API_KEY in your env
print(svar["svar"])

Measured on LongMemEval-S, 500 questions, with gpt-6-luna answering and judged exactly as in the LongMemEval paper (GPT-4o as judge, the official judge prompts): 481 of 500 correct (96.2 %). Our own automatic judging of the same answers gives 96.6 % (previous helper version 88.0 %).

On 200 LoCoMo questions held out and never read during development, v6.2 answered 170 correctly, against 148 for v6.1. The gain is on questions about a person the conversation says nothing about: v6.2 checks which speaker a fact belongs to before answering.

Read those figures with three caveats. The rules in the helper were written against the same 500 LongMemEval questions. The answer model is our own choice, so the judging is comparable with other published LongMemEval scores but the setup is not identical. And the figures depend on the answer model.

escalation_context() returns the raw context, so you can also run your own model call on it instead of the helper.

Energy and time

A model spends energy and time on every token it reads. Measured 2026-11-17 with the GPU's own energy counter (NVIDIA GeForce RTX 4070, llama3.2:1b, idle power subtracted), 50 LongMemEval-S questions, against the whole history (median 104 652 tokens): 95 % less energy and 94.4 % less time for the model to read the context, with the selection at budget 32 000 and every session holding the answer included in 90 % of questions.

The model reads Tokens (median) Less energy Less time All evidence included
Selection, budget 32 000 18 248 95 % 94.4 % 90 %
Selection, budget 16 000 12 672 97.2 % 96.5 % 80 %
Selection, budget 8 000 6 158 98.7 % 97.9 % 50 %
Selection, budget 4 000 3 094 99.3 % 98.5 % 22 %
Selection, budget 2 000 1 080 99.7 % 98.8 % 14 %, 9 empty
Escalation context 75 066 39.3 % 37.4 % 98 %

Per reading of the context, on one GPU with a small model; larger models have not been measured. A shorter history leaves less to remove. The escalation helper reads its context at least twice per question. If the budget is too small for the best match, the response says so in budget_warning (in context() results: .raw["budget_warning"]) and suggests a budget that fits it.

What this does not do

It does not call your model. ctx.text goes to Claude, GPT or whatever you use. This library never sees your model provider's keys.

It is not a proxy. Your uptime does not depend on ours for the model call itself.

Measure before you commit

No account, no key:

from cap_shield import measure, print_measurement

print_measurement(measure(
    texts=["...your actual messages..."],
    query="what your agent would search for"))

Runs on your own traffic and stores nothing — the text is compressed in memory, the numbers computed, and everything discarded. No dictionary is trained on it.

Rate limited to 20 measurements per hour per IP. Enough to evaluate, not enough to use the service for free.

Two mechanisms, deliberately separate

context() saves tokens — what your model is billed for. pack() saves bytes — bandwidth and storage. The packet is decompressed before the model sees it, so it saves no tokens.

Conflating them is the most common misunderstanding about this product.

Full documentation

https://cap-shield-robin.fly.dev/docs/quickstart

MCP server

The package also contains an MCP server. Nine tools, no extra dependencies, and three of them work without an account.

pip install cap-shield
{
  "mcpServers": {
    "cap-shield": {
      "command": "python",
      "args": ["-m", "cap_mcp"],
      "env": {
        "CAP_SHIELD_API_KEY": "cap_live_..."
      }
    }
  }
}

The env block is only needed for remember and assemble_context. Leave it out and measure_traffic and list_packages still work.

Source and docs: https://github.com/robinlidberg-dot/cap-shield-mcp

The server can also be fetched as a single file, for anyone who would rather not install a package:

curl -O https://cap-shield-robin.fly.dev/cap_mcp.py

Metadata

Release files for cap-shield 0.6.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 cap-shield 0.6.1
File Size Uploaded Provenance Actions
cap_shield-0.6.1.tar.gz 95.1 kB Without provenance Details for cap_shield-0.6.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for cap-shield 0.6.1
File Interpreter ABI Platform Actions
cap_shield-0.6.1-py3-none-any.whl Python 3 none any Details for cap_shield-0.6.1-py3-none-any.whl

Total release size: 193.0 kB

Release files / cap_shield-0.6.1.tar.gz

Download URL cap_shield-0.6.1.tar.gz
Size 95.1 kB
Tags Source
SHA-256 checksum
How to use checksums
c34340f3a76752be5b0328c4b12316fbd873fb02591b9f7c5e4d1f75caf2bdda
BLAKE2b-256 checksum
How to use checksums
f5c738f9a7415cb5c9cfeb17c18baa14be04e35d3c412ed108027f65b11c1506
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Release files / cap_shield-0.6.1-py3-none-any.whl

Download URL cap_shield-0.6.1-py3-none-any.whl
Size 97.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
503a8796e8a9ac290e89a56e4b18c72daa3d5cc0f0d100c4731bfcf6b087af91
BLAKE2b-256 checksum
How to use checksums
24ec857f93208cb46ff68575cd9b4ebaf0a51947a1f94e6aa1c98734c3dcf550
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.12.10

Security for cap-shield 0.6.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.

No provenance found

None of the files in this release were uploaded with provenance metadata.

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.

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