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.2.0.tar.gz (16.1 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.2.0-py3-none-any.whl (10.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for sutram-0.2.0.tar.gz
Algorithm Hash digest
SHA256 d3680db437124143b5493216147d41da12b1f0ba5de3073383616ccfae9c01f9
MD5 d5f2d4cfc6126a00786f9442233ebc02
BLAKE2b-256 cc0ca4e3f18ee56c08f47b4a192991de3c3a50e1d50bade196779232cc853144

See more details on using hashes here.

File details

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

File metadata

  • Download URL: sutram-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 10.3 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.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 875343f2d1bf14dcace376b36845ae21c1550dd1aeec64f1f9f14c004bb4e1c6
MD5 cc85a176b84ea0fa8e453c5f8d3257c3
BLAKE2b-256 17029177f8c8778fb21ee0f10fddd295e21d710745dab994d0ebe609bcb5c7f9

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