Skip to main content

Reasoning-aware context runtime for RAG — chunk, retrieve, and allocate the document context an LLM should see, with citations and a Decision Report. In-process, no vector DB.

Project description

RedHop (Python)

Reasoning-preserving context optimization and retrieval observability for RAG systems.

RedHop sits between retrieval and generation. You hand it the chunks your retriever returned and a token budget; it assembles the prompt context — pruning distractors, preserving reasoning-critical "second-hop" evidence, and reporting exactly what it did. It is not a retriever, vector DB, agent framework, or workflow engine.

import redhop

chunks = retriever.retrieve(query)          # your stack
ctx = redhop.build_context(
    query=query,
    retrieved_chunks=chunks,                # list of dicts or strings
    strategy="reasoning_preserving",        # the safe default
    token_budget=12000,
)
response = llm.generate(ctx.text())         # your stack
print(ctx.report)                           # observability ↓
Context Optimization Report
───────────────────────────
Strategy: ReasoningPreserving

Input chunks:        8
Output chunks:       2
Tokens:              100 → 30  (-70%)
Distractors pruned:  6
Reasoning rescues:   1

Evidence density:    0.10 → 0.20
Retained evidence:   100%
...

Installation

pip install redhop          # alpha wheels (PyPI)

The wheel bundles the compiled Rust engine — no Rust toolchain needed to use it.

Quickstart

retrieved_chunks accepts plain Python — a list of dicts (only text is required) or strings:

chunks = [
    {"id": "c1", "text": "...", "score": 0.82},
    {"id": "c2", "text": "..."},
    "a bare string also works",
]
ctx = redhop.build_context(query="...", retrieved_chunks=chunks)
print(ctx.text())              # the assembled prompt context
r = ctx.report
print(r.total_tokens, r.distractors_pruned, r.second_hop_rescue_count)
print(redhop.report_to_dict(r))   # full telemetry as a dict

API surface

Function Purpose
build_context(query, chunks, strategy, token_budget, ...) budget-aware assembly → BuiltContext
filter_context(query, chunks, strategy, ...) filter junk, no budget truncation → BuiltContext
analyze_context(query, chunks, ...) non-destructive diagnostics → ContextReport
context_economics(query, chunks, ...) economics of a set as-is → dict

BuiltContext: .text(), .chunks, .report. ContextReport: .strategy, .total_tokens, .distractors_pruned, .second_hop_rescue_count, .evidence_density, … ; str(report) is the rendered report; redhop.report_to_dict(report) is the full telemetry.

Strategies

strategy what it does when
reasoning_preserving (default) keep query-relevant seeds and rescue low-relevance chunks linked to a seed; drop only unlinked junk multi-hop / general; safe default
distractor_filtered drop everything below a query-grounding bar single-hop, or a low threshold only
max_density greedily pack the densest chunks into the budget single-hop / brutal budgets
raw_topk keep retrieval order until the budget fills baseline / no optimization

Why reasoning_preserving is the default — the second-hop tax

On multi-hop questions, the second hop (the evidence that bridges to the answer) is low-relevance-to-the-query by construction — it connects through a bridge entity, not the query terms. So every relevance-based operation (aggressive distractor filtering, cross-encoder reranking, max-density pruning) drops it. We measured this directly: a relevance filter keeps 96.8% of second hops at threshold 0.05 but only 43.9% at 0.30.

reasoning_preserving resists this: it keeps the query-relevant seeds, then rescues low-relevance chunks that are linked to a seed (sharing the bridge entity), dropping only true junk. End-to-end (n=300, generator = haiku) it beat aggressive filtering with a CI-significant margin, and the gain was causally localized to the rescued evidence.

Transformers tolerate irrelevant context far better than they tolerate missing reasoning links. Premature removal of low-relevance reasoning evidence hurts more than the distractors do.

Examples

python examples/basic_rag.py            # retrieval → build_context → generation
python examples/compare_strategies.py   # every strategy side-by-side
python examples/economics_demo.py       # context economics + analyze_context

Evidence

Every default is grounded in a measured finding (with a falsified-hypotheses registry) in the repo's evidence layer:

  • Second-hop tax: docs/findings/SECOND_HOP_TAX.md
  • Reasoning preservation (end-to-end QA): docs/findings/REASONING_PRESERVATION.md
  • Context economics: docs/findings/CONTEXT_ECONOMICS.md
  • Index: docs/findings/README.md

Local development

# from python/, inside a virtualenv with maturin installed
pip install maturin
maturin develop --release      # builds the Rust engine + installs `redhop` editable
python -c "import redhop; print(redhop.__version__)"

maturin build --release        # produce a wheel in target/wheels/

Rust is the source of truth: this package is a thin pyo3 binding over the redhop-context crate — no logic is duplicated in Python.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

redhop-0.1.0-cp39-abi3-win_amd64.whl (12.7 MB view details)

Uploaded CPython 3.9+Windows x86-64

redhop-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl (16.5 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ x86-64

redhop-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl (16.1 MB view details)

Uploaded CPython 3.9+manylinux: glibc 2.28+ ARM64

redhop-0.1.0-cp39-abi3-macosx_11_0_arm64.whl (13.8 MB view details)

Uploaded CPython 3.9+macOS 11.0+ ARM64

redhop-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl (15.1 MB view details)

Uploaded CPython 3.9+macOS 10.12+ x86-64

File details

Details for the file redhop-0.1.0-cp39-abi3-win_amd64.whl.

File metadata

  • Download URL: redhop-0.1.0-cp39-abi3-win_amd64.whl
  • Upload date:
  • Size: 12.7 MB
  • Tags: CPython 3.9+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for redhop-0.1.0-cp39-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 fcf324d4fecf63097f41b26dfe8662cf4c7672e0740645728aa4e11bd67977cb
MD5 4dc44031a6371120cc47624f097a75d6
BLAKE2b-256 fb1f032799c4fc56521f6e1cc0ce4592800428f06a70cde465563fd0dc5dff10

See more details on using hashes here.

Provenance

The following attestation bundles were made for redhop-0.1.0-cp39-abi3-win_amd64.whl:

Publisher: release-python.yml on vysakh0/redhop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file redhop-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for redhop-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 bc57d077d84df4935aab7118ed6603144be8cfd574825297d5d94d1f7d80c423
MD5 8a50f06a3e5adfe5e1e64167df84b998
BLAKE2b-256 332634b74ca35a1189b8ded4218afacd46e465ff8201619c49c4fd2c7ad94850

See more details on using hashes here.

Provenance

The following attestation bundles were made for redhop-0.1.0-cp39-abi3-manylinux_2_28_x86_64.whl:

Publisher: release-python.yml on vysakh0/redhop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file redhop-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl.

File metadata

File hashes

Hashes for redhop-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl
Algorithm Hash digest
SHA256 21e3da521ea95e02df12b36a0b8569712c9d72e2cd6e1b376e2a222bbcf2d59d
MD5 935599a527357b0c51666a288cebe3f0
BLAKE2b-256 dc7d787054ec3117fc4aa0aeb0c361f64814a2a45a7b15a03e2af4baed569693

See more details on using hashes here.

Provenance

The following attestation bundles were made for redhop-0.1.0-cp39-abi3-manylinux_2_28_aarch64.whl:

Publisher: release-python.yml on vysakh0/redhop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file redhop-0.1.0-cp39-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for redhop-0.1.0-cp39-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 da0fd62ea63a402164a626c27f27e225f729b63ac58f82da69b29baaf8df8552
MD5 807bba4f262e294dc165b38cb1af1360
BLAKE2b-256 b13d8281b325acade953bb9f4e8e63b412f0d6249f0b068f565fb4f8894c729e

See more details on using hashes here.

Provenance

The following attestation bundles were made for redhop-0.1.0-cp39-abi3-macosx_11_0_arm64.whl:

Publisher: release-python.yml on vysakh0/redhop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file redhop-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for redhop-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 3f47ea7ae0a237a5a14dcb656c3415624b0ae76efe0023d9f8863bde31946d6f
MD5 23c52ea46a6a252d5b92be0c3008a044
BLAKE2b-256 62f3d288482bdd0a87411f749547af33a9a6b42abb59cc95d7a0f0e09f10767c

See more details on using hashes here.

Provenance

The following attestation bundles were made for redhop-0.1.0-cp39-abi3-macosx_10_12_x86_64.whl:

Publisher: release-python.yml on vysakh0/redhop

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page