Skip to main content

AI API gateway SDK with cost tracking, budget enforcement, and multi-provider routing for LLM applications

Project description

llmkit

Python SDK for LLMKit, the AI API gateway with cost tracking and budget enforcement.

Install

pip install llmkit-sdk  # import as: from llmkit import ...

Local cost tracking (no proxy)

Drop-in cost tracking for any OpenAI-compatible SDK. No account, no proxy, no config:

from llmkit import tracked
from openai import OpenAI

client = OpenAI(http_client=tracked())
response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "hello"}],
)
# costs estimated locally from bundled pricing table

Collect costs with a callback:

costs = []
client = OpenAI(http_client=tracked(on_cost=costs.append))
# ... make requests ...
total = sum(c.total_cost for c in costs if c.total_cost)

Or estimate from any existing response:

from llmkit import estimate_cost

cost = estimate_cost(response)
if cost.total_cost is not None:
    print(f"~${cost.total_cost:.6f}")

Works with OpenAI, Anthropic, Groq, Together, Cohere, and Mistral SDKs.

Quick start (proxy mode)

from llmkit import LLMKit

client = LLMKit(api_key="llmk_...")
completion, cost = client.chat(
    model="gpt-4o",
    messages=[{"role": "user", "content": "hello"}],
)

print(completion.choices[0].message.content)
print(f"Cost: ${cost.total_cost:.4f} via {cost.provider}")

Session tracking

Group costs by agent run using sessions:

agent = client.session()

for task in tasks:
    completion, cost = agent.chat(
        model="gpt-4o",
        messages=[{"role": "user", "content": task}],
    )

print(f"Session total: ${agent.stats.total_cost:.4f}")
print(f"Requests: {agent.stats.request_count}")

Bring your own provider key

client = LLMKit(
    api_key="llmk_...",
    provider_key="sk-...",
)

Streaming with cost tracking

stream = client.chat_stream(
    model="gpt-4o",
    messages=[{"role": "user", "content": "hello"}],
)
for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

print(f"\nCost: ${stream.cost.total_cost:.6f}")

Cost is captured from the final stream chunk's usage data. Cumulative totals are available on client.stats.

Escape hatch

The underlying OpenAI client is always accessible for anything the SDK doesn't cover:

stream = client.openai.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "hello"}],
    stream=True,
)

for chunk in stream:
    print(chunk.choices[0].delta.content or "", end="")

Async

from llmkit import AsyncLLMKit

client = AsyncLLMKit(api_key="llmk_...")
completion, cost = await client.chat(
    model="gpt-4o",
    messages=[{"role": "user", "content": "hello"}],
)

No SDK needed

LLMKit works with any OpenAI-compatible client. No pip install required:

from openai import OpenAI

client = OpenAI(
    base_url="https://llmkit-proxy.smigolsmigol.workers.dev/v1",
    api_key="llmk_...",
)

response = client.chat.completions.create(
    model="gpt-4o",
    messages=[{"role": "user", "content": "hello"}],
)

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

llmkit_sdk-0.1.6.tar.gz (23.6 kB view details)

Uploaded Source

Built Distribution

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

llmkit_sdk-0.1.6-py3-none-any.whl (17.9 kB view details)

Uploaded Python 3

File details

Details for the file llmkit_sdk-0.1.6.tar.gz.

File metadata

  • Download URL: llmkit_sdk-0.1.6.tar.gz
  • Upload date:
  • Size: 23.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for llmkit_sdk-0.1.6.tar.gz
Algorithm Hash digest
SHA256 17644857dede832c580e102d461eb42f1a94d4c556ea25e6547b30c2f67dc899
MD5 1addab08eecf237edc90578f85b4365f
BLAKE2b-256 adde0abd040b7b9c495598ac9d6795d35dbd2d927baa9291a0046e98430eeb31

See more details on using hashes here.

File details

Details for the file llmkit_sdk-0.1.6-py3-none-any.whl.

File metadata

  • Download URL: llmkit_sdk-0.1.6-py3-none-any.whl
  • Upload date:
  • Size: 17.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.4

File hashes

Hashes for llmkit_sdk-0.1.6-py3-none-any.whl
Algorithm Hash digest
SHA256 caaea3136a6cf584f45417cfdf8e6a289ba2760adf2b148df737fc40ff03e20e
MD5 f90f00f13b91ff9b5a87f1edb2d1b6fd
BLAKE2b-256 d7c6204f8a9b59253698c6971d80208036f4ad7df15d61a0cac1e582701af807

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