Skip to main content

Intelligent context routing engine for AI agents

Project description

Intelligent context routing for AI agents — YAML configs, multi-source retrieval, any platform.

License PyPI Docs Follow @CohorteAI

[!NOTE] Part of the theaios ecosystem. Install with pip install theaios-context-router.

What It Does

Define how AI agents discover and receive context in YAML. The engine routes each query to the right sources, enforces agent permissions, fetches content in parallel, scores relevance, and trims to a token budget. No manual prompt-stuffing. Deterministic, fast, auditable.

  • YAML configuration — declare sources, routes, permissions, and budgets in a single file
  • Multi-source retrieval — inline content, local directories, git repos, REST APIs
  • Expression-based routing — route queries to sources using a safe expression language (text contains "policy", boolean logic, variables)
  • Agent permissions — per-agent allow/deny lists for sources and file paths
  • Token budget management — relevance ranking, configurable truncation strategies (drop, truncate_end, truncate_middle)
  • Automatic markdown splitting — splits .md files by H2 headings for fine-grained retrieval
  • Disk cache with TTL — cache source results to avoid redundant fetches
  • Extensible sources — add custom sources via @register_source plugin system
  • CLI tools — validate configs, inspect routers, run queries from the terminal

Quick Start

pip install theaios-context-router

1. Write a config:

# context-router.yaml
version: "1.0"

sources:
  system_prompt:
    type: inline
    content: "You are a helpful assistant. Be concise."
    priority: 10

  docs:
    type: directory
    path: "./data"
    patterns: ["**/*.md", "**/*.txt"]

routes:
  - name: default
    when: ""
    sources: [system_prompt, docs]

  - name: policy-questions
    when: 'text contains "policy"'
    sources: [docs]

budget:
  max_tokens: 4000
  ranking: relevance
  truncation: drop

2. Use it:

from theaios.context_router import Router, load_config, Query

config = load_config("context-router.yaml")
router = Router(config)

response = router.query(Query(text="What is the remote work policy?"))

print(response.matched_routes)  # ["policy-questions", "default"]
print(len(response.chunks))     # 3
print(response.total_tokens)    # 847

Queries tell the engine what context to find. Each query has a text (what to search for), an agent (who is asking), and optional tags and metadata:

# Basic query
router.query(Query(text="How do expenses work?"))

# Agent-specific query (permissions apply)
router.query(Query(text="Show me project docs", agent="eng-assistant"))

# Query with metadata (accessible in route expressions)
router.query(Query(
    text="Find onboarding guide",
    agent="hr-bot",
    tags=["onboarding"],
    metadata={"department": "engineering"},
))

The engine evaluates route conditions against the query, filters by permissions, fetches from allowed sources in parallel, scores relevance, and trims to the token budget. The ContextResponse contains the ranked chunks, metadata, and timing information.

Or one-liner:

from theaios.context_router import query

response = query("context-router.yaml", text="What is the PTO policy?")

3. CLI:

context-router validate --config context-router.yaml
context-router inspect --config context-router.yaml
context-router query --config context-router.yaml --text "What is the remote work policy?"
context-router query --config context-router.yaml --text "expenses" --output json
context-router cache stats --config context-router.yaml
context-router cache clear --config context-router.yaml

Why This Library?

Every AI agent needs context. The options today:

Approach Problem
Manual prompt-stuffing Hardcoded, doesn't scale, no permissions
RAG frameworks (LlamaIndex, LangChain) Heavy dependencies, vector DB required, complex setup
Vendor context windows (Gemini, Claude) Locked to one provider, no access control
Build your own Weeks of engineering, no standard format

theaios-context-router is lightweight (pure Python, no vector DB), declarative (YAML configs that teams can review), permission-aware (per-agent access control), and deterministic (same query = same context, every time).

Source Types

Source Description Config Key
inline Static content embedded in config content
directory Read files from a local directory path, patterns
git_repo Read files from a git ref path, ref, patterns
http_api Query a REST API endpoint url, method, body_template

Custom sources: implement the Source base class and register with @register_source("my_source").

Generate Configs with AI

Don't want to write YAML by hand? Use any LLM to generate a config. Copy-paste one of our ready-made prompts and get a production-ready YAML file in seconds. Prompts are included for:

  • Generating a full config from scratch (the AI asks about your sources and agents)
  • Extending an existing config with new sources or routes
  • Converting plain-English routing rules to YAML
  • Security-auditing an existing config for permission gaps

Then validate: context-router validate --config generated-config.yaml

Documentation

Full documentation at cohorte-ai.github.io/context-router — including the configuration reference, source types, expression language, permissions, and budget management.

Part of the theaios Ecosystem

theaios-context-router is one of the theaios platform components. It works standalone or alongside:

License

Apache 2.0 — see LICENSE.

Project details


Download files

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

Source Distribution

theaios_context_router-0.1.0.tar.gz (105.4 kB view details)

Uploaded Source

Built Distribution

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

theaios_context_router-0.1.0-py3-none-any.whl (40.0 kB view details)

Uploaded Python 3

File details

Details for the file theaios_context_router-0.1.0.tar.gz.

File metadata

  • Download URL: theaios_context_router-0.1.0.tar.gz
  • Upload date:
  • Size: 105.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.1

File hashes

Hashes for theaios_context_router-0.1.0.tar.gz
Algorithm Hash digest
SHA256 213c35954ec54cc5443663d7f3ca5a83519cbbb374d261e567a0f5a45fa08791
MD5 dd893c218e3527c5d85b63146d12e0d3
BLAKE2b-256 6badd7013817150a32cc7f3c5ed76f46f7baeee8aa3364332028c82a7ff029d2

See more details on using hashes here.

File details

Details for the file theaios_context_router-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for theaios_context_router-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a2606f0e814c34350724ef8107950d4fd9ac43de13d8df839d99ea1f037a5916
MD5 28c678a27b2896e445e4fb9b0594fab0
BLAKE2b-256 2f04d16430ce17e968ffa892d7476008cb6f01293e803a0dd3d8e618b5e0e251

See more details on using hashes here.

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