Skip to main content

thanos (Python)

Keep an LLM conversation inside its context window and pricing tier, track spend, and compress history automatically when a limit is hit.

pip install thanos-ctx

(Published as thanos-ctx — the bare thanos name on PyPI belongs to an unrelated project. The import stays from thanos import ....)

from thanos import ContextGuard

guard = ContextGuard(model="claude-sonnet-4-5")

messages = [
    {"role": "system", "content": "You are a helpful assistant."},
    {"role": "user", "content": "..."},
    # ... growing conversation history
]

result = guard.prepare(messages)

# Send result.messages to the API instead of the original list.
response = client.messages.create(model="claude-sonnet-4-5", messages=result.messages, ...)

print(result.tokens, result.estimated_cost_usd, result.compressed)

Budgets

from thanos import Budget, ContextGuard

budget = Budget(max_cost_usd=5.00)
guard = ContextGuard(model="claude-sonnet-4-5", budget=budget)

for turn in conversation:
    result = guard.prepare(turn.messages)  # auto-compresses once max_cost_usd is projected to be exceeded

Custom compression

from thanos import ContextGuard

def summarize_and_truncate(messages, target_tokens, pinned_prefix_len, tokenizer):
    # Your own strategy — e.g. call an LLM to summarize the dropped turns
    # into one system note, then fall back to truncation.
    ...

guard = ContextGuard(model="claude-sonnet-4-5", compressor=summarize_and_truncate)

Reacting to limits

def alert(info: dict) -> None:
    print(f"compressed={info['compressed']} tokens={info['tokens']} cost=${info['estimated_cost_usd']:.4f}")

guard = ContextGuard(model="claude-sonnet-4-5", on_limit=alert)

Pass raise_on_limit=True to raise thanos.LimitExceeded instead of just notifying on_limit.

Adding or overriding a model

guard = ContextGuard(
    model="my-self-hosted-model",
    models={
        "my-self-hosted-model": {
            "contextWindow": 128_000,
            "tiers": [{"maxTokens": 128_000, "inputPerMTok": 0.0, "outputPerMTok": 0.0}],
        }
    },
)

See the top-level README for the full design rationale.

Metadata

Release files for thanos-ctx 0.1.0

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

Source distribution (sdist)

Source distribution for thanos-ctx 0.1.0
File Size Uploaded Provenance Actions
thanos_ctx-0.1.0.tar.gz 20.9 kB Without provenance Details for thanos_ctx-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for thanos-ctx 0.1.0
File Interpreter ABI Platform Actions
thanos_ctx-0.1.0-py3-none-any.whl Python 3 none any Details for thanos_ctx-0.1.0-py3-none-any.whl

Total release size: 39.6 kB

Release files / thanos_ctx-0.1.0.tar.gz

Download URL thanos_ctx-0.1.0.tar.gz
Size 20.9 kB
Tags Source
SHA-256 checksum
How to use checksums
71856bb9649bc0677359ea487bf6e6ad47d3ca861a8ad092eed0d4e67e32909c
BLAKE2b-256 checksum
How to use checksums
c3c08e5a95766ace0047e55dd2fa6bba08472b75704a5fe119b9df3023563418
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Release files / thanos_ctx-0.1.0-py3-none-any.whl

Download URL thanos_ctx-0.1.0-py3-none-any.whl
Size 18.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8a58774a483debb58e7775d22e1553e7cc1a139ad6549b469faac41f3cd3a262
BLAKE2b-256 checksum
How to use checksums
9f98b957a51579f11531b729cb9b413b2df94fe8d203e1d97bc7c829bfb1cdf7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.15.2

Security for thanos-ctx 0.1.0

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.

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files No 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