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.1.tar.gz (13.0 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.1-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sutram-0.1.1.tar.gz
Algorithm Hash digest
SHA256 36dd877efd4346141ead16698ecb423269b785de4bbe4bfc46e7b804aabad045
MD5 192b96bb338068407eacede9c03ee85e
BLAKE2b-256 f555e96abd18b7255e39ccbfe045faedcde7b5774accf08e2055b3cdfbc542b4

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for sutram-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd34edba4be8be65f0f1e38c29d15dc0917c1903523888cc56bcff29ea371456
MD5 82b253ad9e91ca7d647d0d0456351e70
BLAKE2b-256 980426a98f8a7e477f437171b25c1c692204370f94730b8ea1efc17acb39ba5e

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