Skip to main content

Basis Router

A routing system for providing context to LLM requests across multiple providers (Anthropic, OpenAI, Gemini) with intelligent chunking and result aggregation.

Installation

Basic Installation

pip install basis-router

Quick Start

The router follows a simple setup-then-use pattern:

from router import (
    Router,
    ModelConfig,
    ChunkingConfig,
    DataSourceType,
    S3StoreConfig,
    MongoDBStoreConfig,
    PostgresStoreConfig,
)
import os

# Setup Step #1: Initialize router
router = Router(
    config={
        "api_keys": {
            "anthropic": os.getenv("ANTHROPIC_API_KEY"),
            "openai": os.getenv("OPENAI_API_KEY"),
        }
    }
)

# Setup Step #2: Connect data sources
router.connect_data_source(
    label="my_s3_bucket",
    source_type=DataSourceType.S3,
    store_config=S3StoreConfig(
        region="us-east-1",
        bucket="my-bucket",
        access_key=os.getenv("AWS_ACCESS_KEY"),
        secret_key=os.getenv("AWS_SECRET_KEY"),
    ),
)

router.connect_data_source(
    label="my_postgres",
    source_type=DataSourceType.POSTGRES,
    store_config=PostgresStoreConfig(
        connection_url=os.getenv("POSTGRES_URL"),
    ),
)

# Setup Step #3: Add routing rules
router.add_rule(
    name="evaluate_agent", # task name
    model_config=ModelConfig(
        provider="anthropic",
        model="claude-sonnet-4-20250514",
        temperature=0.2
    ),
    chunking_config=ChunkingConfig(
        enabled=True,
        chunk_size=8000,
        aggregation="concatenate"
    )
)

# Usage: Route using simple labels!
response = await router.route(
    store_label="my_s3_bucket",
    query="logs/agent-interaction-123.json", # actual data to route
    rule_name="evaluate_agent", # routing rule to route by
)

print(f"Result: {response.result}")
print(f"Cost: ${response.metadata['total_cost']}")
print(f"Chunks processed: {response.metadata['chunks_processed']}")

Workflow

The router uses a three-step setup process:

  1. Initialize Router: Configure API keys and default settings
  2. Connect Data Sources: Register your data stores (S3, MongoDB, Postgres, etc.)
  3. Add Rules: Define routing rules that specify which model to use for different tasks

Once setup is complete, routing is simple: just specify the data source label, query, and rule name.

Configuration types

Router Config

config = {
    "default_chunk_size": 8000,
    "default_overlap": 500,
    "api_keys": {
        "anthropic": "your-key",
        "openai": "your-key"
    },
    "system_prompt": "You are a helpful assistant.",
    "prompt": "Evaluate the following text: {text}"
}

ModelConfig

ModelConfig(
    provider="anthropic",  # or "openai", "gemini"
    model="claude-sonnet-4-20250514",
    temperature=0.7,
    max_tokens=4096,
    system_prompt="Optional system prompt"
)

ChunkingConfig

ChunkingConfig(
    enabled=True,
    strategy="fixed_size",  # "fixed_size", "semantic", "sliding_window"
    chunk_size=8000,
    overlap=500,
    aggregation="concatenate"  # "concatenate", "majority_vote", "average_score"
)

Data Source Types

Connect data sources using connect_data_source(). Supported types:

  • S3: Amazon S3 storage
  • MONGODB: MongoDB database
  • POSTGRES: PostgreSQL database
  • DYNAMODB: Amazon DynamoDB
  • JSON_FILE: Local JSON files
  • CONTENT: Direct content strings

Note: GCS (Google Cloud Storage) support is not currently implemented but can be added in the future.

Store Configurations

Each data source type has a corresponding store config class:

from router import (
    DataSourceType,
    S3StoreConfig,
    MongoDBStoreConfig,
    PostgresStoreConfig,
    DynamoDBStoreConfig,
    JsonFileStoreConfig,
)

# S3
router.connect_data_source(
    label="my_s3",
    source_type=DataSourceType.S3,
    store_config=S3StoreConfig(
        region="us-east-1",
        bucket="my-bucket",
        access_key="...",
        secret_key="...",
    ),
)

# MongoDB
router.connect_data_source(
    label="my_mongodb",
    source_type=DataSourceType.MONGODB,
    store_config=MongoDBStoreConfig(
        connection_string="mongodb://localhost:27017",
        database="mydb",
    ),
)


# Postgres
router.connect_data_source(
    label="my_postgres",
    source_type=DataSourceType.POSTGRES,
    store_config=PostgresStoreConfig(
        connection_url="postgresql://user:pass@host:port/db",
    ),
)

Query Format

The query parameter in route() varies by data source type:

  • S3: File path in bucket (e.g., "logs/agent-123.json")
  • MongoDB: Query as JSON string (e.g., '{"agent_id": "123"}') or collection name
  • Postgres: SQL query (e.g., "SELECT content FROM logs WHERE id = 123")
  • JSON_FILE: File path (e.g., "data/logs.json")
  • CONTENT: The content string itself

Examples

Complete Example

from router import (
    Router,
    ModelConfig,
    ChunkingConfig,
    DataSourceType,
    S3StoreConfig,
    MongoDBStoreConfig,
    PostgresStoreConfig,
)
import os

# Setup Step #1: Initialize router
router = Router(
    config={
        "api_keys": {
            "anthropic": os.getenv("ANTHROPIC_API_KEY"),
            "openai": os.getenv("OPENAI_API_KEY"),
        }
    }
)

# Setup Step #2: Connect data sources
router.connect_data_source(
    label="my_s3_bucket",
    source_type=DataSourceType.S3,
    store_config=S3StoreConfig(
        region="us-east-1",
        bucket="my-bucket",
        access_key=os.getenv("AWS_ACCESS_KEY"),
        secret_key=os.getenv("AWS_SECRET_KEY"),
    ),
)

router.connect_data_source(
    label="my_mongodb",
    source_type=DataSourceType.MONGODB,
    store_config=MongoDBStoreConfig(
        connection_string="mongodb://localhost:27017",
        database="mydb",
    ),
)

router.connect_data_source(
    label="my_postgres",
    source_type=DataSourceType.POSTGRES,
    store_config=PostgresStoreConfig(
        connection_url=os.getenv("POSTGRES_URL"),
    ),
)

# Setup Step #3: Add rules
router.add_rule(
    name="evaluate_agent",
    model_config=ModelConfig(
        provider="anthropic",
        model="claude-sonnet-4-20250514",
        temperature=0.2
    ),
    chunking_config=ChunkingConfig(
        enabled=True,
        chunk_size=8000,
        aggregation="concatenate"
    )
)

router.add_rule(
    name="classify_cheap",
    model_config=ModelConfig(
        provider="anthropic",
        model="claude-haiku-4-20250101",
        temperature=0
    ),
    chunking_config=ChunkingConfig(
        enabled=True,
        aggregation="majority_vote"
    )
)

# Usage: Route using simple labels
response = await router.route(
    store_label="my_s3_bucket",
    query="logs/agent-interaction-123.json",
    rule_name="evaluate_agent",
)

response = await router.route(
    store_label="my_mongodb",
    query='{"agent_id": "123"}',  # MongoDB query as JSON string
    rule_name="classify_cheap",
)

response = await router.route(
    store_label="my_postgres",
    query="SELECT content FROM agent_logs WHERE id = 123",
    rule_name="evaluate_agent",
)

print(f"Result: {response.result}")
print(f"Cost: ${response.metadata['total_cost']}")
print(f"Chunks processed: {response.metadata['chunks_processed']}")

Response Format

The router returns a RouterResponse object:

@dataclass
class RouterResponse:
    result: Any  # The aggregated result
    metadata: Dict[str, Any]  # Cost, tokens, latency, etc.
    chunks: Optional[List[Dict]]  # Individual chunk results

Aggregation Strategies

  • concatenate: Join all chunk results together
  • majority_vote: Return the most common result across chunks
  • average_score: Average numerical scores from chunks

Metadata

Release files for basis-router 0.1.2

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

Source distribution (sdist)

Source distribution for basis-router 0.1.2
File Size Uploaded Provenance
basis_router-0.1.2.tar.gz 25.7 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for basis-router 0.1.2
File Interpreter ABI Platform
basis_router-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 61.8 kB

Release files / basis_router-0.1.2.tar.gz

Download URL basis_router-0.1.2.tar.gz
Size 25.7 kB
Tags Source
SHA-256 checksum
How to use checksums
91b44d15170f63df2728e137d4eb9c241c1cb78e0784086646ce32e1046b3d8e
BLAKE2b-256 checksum
How to use checksums
f8dbac477d455cad1ddf5f9da3036fdb729cd2c60de0587266268007bfedb70a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.1

Release files / basis_router-0.1.2-py3-none-any.whl

Download URL basis_router-0.1.2-py3-none-any.whl
Size 36.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8adec9bc74f0bd951d555a045dc4182a42d00d3526c7203f5bac54666f68c7d6
BLAKE2b-256 checksum
How to use checksums
3bd2ac26f1bc0e7abbb391c52e517cfe5f314cadb74e456e4c1edb5704f4094a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.12.1

Security for basis-router 0.1.2

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.

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