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.content)

# 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.content)
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(),
)

Creating a Custom Provider

For providers that use the OpenAI chat completions format (OpenAI, Groq, Together, Mistral, etc.), extend OpenAICompatProvider — no methods to implement:

from sutram import OpenAICompatProvider, register_provider

@register_provider("openai", base_url="https://api.openai.com/v1/chat/completions")
class OpenAIProvider(OpenAICompatProvider):
    pass

For providers with a different format, extend BaseProvider and implement _build_request_body and _parse_response:

from sutram import BaseProvider, LLMResponse, register_provider

@register_provider("myprovider", base_url="https://api.myprovider.com/v1/chat")
class MyProvider(BaseProvider):
    def _build_request_body(self, messages: list[dict]) -> dict:
        return {"model": self.model, "messages": messages}

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

Now use it like any built-in provider:

provider = create_provider(
    name="myprovider",
    model="my-model",
    api_key="my-key",
)

The base_url in the decorator is optional — you can pass it at creation time instead:

@register_provider("myprovider")
class MyProvider(BaseProvider):
    ...

provider = create_provider(
    name="myprovider",
    model="my-model",
    api_key="my-key",
    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.3.0.tar.gz (17.3 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.3.0-py3-none-any.whl (10.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: sutram-0.3.0.tar.gz
  • Upload date:
  • Size: 17.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sutram-0.3.0.tar.gz
Algorithm Hash digest
SHA256 efb449d917453021689a0b1f40e760fa74f508ff767b9b4df2b49e115c739491
MD5 b1f54a12934479bcb8f15646dd72524d
BLAKE2b-256 e44e9d89ef081a9f7a99ff414047c68ef4eddcf31f625014062a00e89fd5962a

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sutram-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 10.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for sutram-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4ea425e656ad6f5130ced2cf3923d61deeb36e5f803d6beb1a9b55772e0fa2c9
MD5 79755127a306ca7b04a6da06f622908a
BLAKE2b-256 7ea845c9d53d6892af29dfbeab1b95a6e5b86b51574e7f06ddde9f2c825bc24b

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