Skip to main content

A lightweight Python library for calling LLMs through a unified interface with built-in caching, retry policies, and session management

Project description

sūtram (సూత్రం)

The thread that connects

A unified Python interface for LLM providers. One thread to connect your code to any language model — with built-in caching, retry policies, and multi-turn session management.

In Sanskrit, sūtram means "thread" or "formula" — the essential connection that holds everything together.

Features

  • Unified Provider Interface — One API to call OpenRouter, OpenAI, Anthropic, and more
  • Built-in Caching — Avoid redundant API calls with pluggable cache backends
  • Retry Policies — Configurable exponential/fixed backoff with status-code filtering
  • Multi-turn Sessions — First-class support for conversation history management
  • Sync & Async — Full support for both synchronous and asynchronous workflows
  • Extensible — Add new providers by subclassing BaseProvider

Installation

pip install sutram

Quick Start

from sutram import create_provider, Session, DictCache

# Create a provider
provider = create_provider(
    name="openrouter",
    model="openai/gpt-4",
    api_key="your-api-key",
    cache=DictCache(),
)

# Single-turn call
response = provider.call_llm("What is the meaning of sūtram?")
print(response)

# Multi-turn conversation
session = Session(system_prompt="You are a helpful assistant.")
session.add_user_message("Hello!")
response = provider.chat(session.get_messages())
session.add_assistant_message(response)
session.add_user_message("Tell me more.")
response = provider.chat(session.get_messages())

Configuration

from sutram import create_provider, DictCache

provider = create_provider(
    name="openrouter",
    model="openai/gpt-4",
    api_key="your-api-key",
    max_retries=3,
    backoff_factor=1.0,
    strategy="exponential",
    timeout=120,
    retry_on_status=[429, 500, 502, 503, 504],
    cache=DictCache(),
)

Adding a Custom Provider

from sutram import BaseProvider, PROVIDER_REGISTRY

class MyProvider(BaseProvider):
    def _build_request_body(self, messages: list[dict]) -> dict:
        return {"model": self.model, "messages": messages}

    def _parse_response(self, data: dict) -> str:
        return data["choices"][0]["message"]["content"]

PROVIDER_REGISTRY["myprovider"] = {
    "cls": MyProvider,
    "base_url": "https://api.myprovider.com/v1/chat",
}

License

MIT

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

sutram-0.1.0.tar.gz (7.5 kB view details)

Uploaded Source

Built Distribution

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

sutram-0.1.0-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sutram-0.1.0.tar.gz
  • Upload date:
  • Size: 7.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.20

File hashes

Hashes for sutram-0.1.0.tar.gz
Algorithm Hash digest
SHA256 dbb64b3e98329de09af27d03090821b214741d1680994ad3ea46f7add06b81d7
MD5 bb77f6d334283c8717153affb02a7a0c
BLAKE2b-256 fec88bf6ee361ff251a2c389629d79b7677e5ddaebadc3a4ad3c14e5a0a2022a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sutram-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.20

File hashes

Hashes for sutram-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f001c6673117052fd00d12f20a585252d2f1a2c2b9928c0ca7c9fa9502dff6f5
MD5 7f5fa1f568d97d876e387b79a620ef19
BLAKE2b-256 05c6666ca8ca0c1b908258db3e81e1561218b32b7c21d4be7e6fa31fd993d554

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