Skip to main content

Unified Python interface for multiple LLM providers with cost tracking

Project description

majordomo-llm

PyPI version Python 3.12+ License: MIT

A unified Python interface for multiple LLM providers with automatic cost tracking, retry logic, and structured output support.

Features

  • Unified API - Same interface for OpenAI, Anthropic (Claude), Google Gemini, DeepSeek, and Cohere
  • Cost Tracking - Automatic calculation of input/output token costs per request
  • Structured Outputs - Native support for Pydantic models as response schemas
  • Automatic Retries - Built-in exponential backoff retry logic using tenacity
  • Async First - Fully async/await compatible for high-performance applications
  • Type Safe - Complete type annotations and py.typed marker for IDE support

Installation

pip install majordomo-llm

Or with uv:

uv add majordomo-llm

Quick Start

Basic Text Response

import asyncio
from majordomo_llm import get_llm_instance

async def main():
    # Create an LLM instance
    llm = get_llm_instance("anthropic", "claude-sonnet-4-20250514")

    # Get a response
    response = await llm.get_response(
        user_prompt="What is the capital of France?",
        system_prompt="You are a helpful geography assistant.",
    )

    print(response.content)
    print(f"Tokens: {response.input_tokens} in, {response.output_tokens} out")
    print(f"Cost: ${response.total_cost:.6f}")

asyncio.run(main())

JSON Response

response = await llm.get_json_response(
    user_prompt="List the top 3 largest countries by area as JSON",
    system_prompt="Respond with valid JSON only.",
)

# response.content is a parsed Python dict
for country in response.content["countries"]:
    print(country["name"])

Structured Output with Pydantic

from pydantic import BaseModel

class CountryInfo(BaseModel):
    name: str
    capital: str
    population: int
    area_km2: float

response = await llm.get_structured_json_response(
    response_model=CountryInfo,
    user_prompt="Give me information about Japan",
)

# response.content is a validated CountryInfo instance
country = response.content
print(f"{country.name}: {country.capital}, pop. {country.population:,}")

Configuration

Environment Variables

Set API keys for the providers you want to use:

# OpenAI
export OPENAI_API_KEY="sk-..."

# Anthropic (Claude)
export ANTHROPIC_API_KEY="sk-ant-..."

# Google Gemini
export GEMINI_API_KEY="..."

# DeepSeek
export DEEPSEEK_API_KEY="sk-..."

# Cohere
export CO_API_KEY="..."

Available Models

OpenAI

  • gpt-5, gpt-5-mini, gpt-5-nano
  • gpt-4o, gpt-4.1, gpt-4.1-mini, gpt-4.1-nano

Anthropic

  • claude-sonnet-4-5-20250929, claude-opus-4-1-20250805
  • claude-opus-4-20250514, claude-sonnet-4-20250514
  • claude-3-7-sonnet-latest, claude-3-5-haiku-latest

Gemini

  • gemini-2.5-flash, gemini-2.5-flash-lite
  • gemini-2.0-flash, gemini-2.0-flash-lite

DeepSeek

  • deepseek-chat, deepseek-reasoner

Cohere

  • command-a-03-2025, command-r-plus-08-2024
  • command-r-08-2024, command-r7b-12-2024

API Reference

Factory Functions

get_llm_instance(provider: str, model: str) -> LLM

Create an LLM instance for the specified provider and model.

from majordomo_llm import get_llm_instance

llm = get_llm_instance("openai", "gpt-4o")

LLM Methods

All LLM instances support these async methods:

get_response(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0) -> LLMResponse

Get a plain text response.

get_json_response(user_prompt, system_prompt=None, temperature=0.3, top_p=1.0) -> LLMJSONResponse

Get a JSON response (automatically parsed).

get_structured_json_response(response_model, user_prompt, system_prompt=None, temperature=0.3, top_p=1.0) -> LLMStructuredResponse

Get a response validated against a Pydantic model.

Response Objects

All response objects include usage metrics:

Field Type Description
content str / dict / BaseModel The response content
input_tokens int Number of input tokens
output_tokens int Number of output tokens
cached_tokens int Number of cached tokens (if applicable)
input_cost float Cost for input tokens (USD)
output_cost float Cost for output tokens (USD)
total_cost float Total cost (USD)
response_time float Response time in seconds

Advanced Usage

Direct Provider Access

You can also instantiate providers directly for more control:

from majordomo_llm import Anthropic

llm = Anthropic(
    model="claude-sonnet-4-20250514",
    input_cost=3.0,    # per million tokens
    output_cost=15.0,  # per million tokens
)

Web Search (Anthropic)

Enable web search for supported Claude models:

from majordomo_llm.providers.anthropic import Anthropic

llm = Anthropic(
    model="claude-sonnet-4-5-20250929",
    input_cost=3.0,
    output_cost=15.0,
    use_web_search=True,
)

Development

Setup

git clone https://github.com/superset-studio/majordomo-llm.git
cd majordomo-llm
uv sync --all-extras

Running Tests

uv run pytest

Type Checking

uv run mypy src/majordomo_llm

Linting

uv run ruff check src/majordomo_llm

Contributing

Contributions are welcome! Please see CONTRIBUTING.md for guidelines.

License

This project is licensed under the MIT License - see the LICENSE file for details.

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

majordomo_llm-0.1.1.tar.gz (78.6 kB view details)

Uploaded Source

Built Distribution

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

majordomo_llm-0.1.1-py3-none-any.whl (22.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: majordomo_llm-0.1.1.tar.gz
  • Upload date:
  • Size: 78.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for majordomo_llm-0.1.1.tar.gz
Algorithm Hash digest
SHA256 a7b6a4390520fb4d0f47d1db3fc8ff58d5771ba64e9c84a77f19b59b60727baa
MD5 807fc8fc35a8ca1bcb82b586369b8222
BLAKE2b-256 ec869de9d61fc860bf01560adc6637a3113b71551f42373ae1303cd3ef7ad482

See more details on using hashes here.

File details

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

File metadata

  • Download URL: majordomo_llm-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 22.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.10

File hashes

Hashes for majordomo_llm-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 80c5d77980e0578ecf9e2ac7f84025b25231387b24a99634968d8701f5521ac5
MD5 046fb80608a1922f1a4b571b5aaa0aac
BLAKE2b-256 96e43ad2241cb426f141ee925cb426ab0cbf123706b3c8c2741ace210350b023

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