Metadata-Version: 2.5
Name: cap-shield
Version: 0.6.2
Summary: Context selection and compression for AI agents — measured, not estimated
Project-URL: Homepage, https://cap-shield-robin.fly.dev
Project-URL: Documentation, https://cap-shield-robin.fly.dev/docs/quickstart
Author: Robin Lidberg
License: MIT
Keywords: agents,ai,compression,context,llm,memory,rag,retrieval,tokens
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
Requires-Python: >=3.9
Requires-Dist: httpx>=0.24
Description-Content-Type: text/markdown

# cap-shield

Context selection for AI agents. **Measured, not estimated.**

```bash
pip install cap-shield
```

```python
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.

```python
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:**

```python
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-name: io.github.robinlidberg-dot/cap-shield -->

## MCP server

The package also contains an MCP server. Fifteen tools, no extra
dependencies, and four of them work without an account
(`measure_traffic`, `list_packages`, `start_sandbox`, `verify_pass`).

```bash
pip install cap-shield
```

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

The `env` block is only needed for the tools that store or retrieve
memory, pack data or issue passes. Leave it out and `measure_traffic`,
`list_packages`, `start_sandbox` and `verify_pass` still work;
`start_sandbox` gives a 24-hour key for trying memory without an account.

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:

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