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.5.tar.gz (22.8 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.5-py3-none-any.whl (17.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llmkit_sdk-0.1.5.tar.gz
  • Upload date:
  • Size: 22.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llmkit_sdk-0.1.5.tar.gz
Algorithm Hash digest
SHA256 0bd93acb0efb2f2c155077076cf40626dde4538eba133eb6ee8f81a5c2d0b8bc
MD5 e48651eb79a6de4dcfb0e1973e9ef6c7
BLAKE2b-256 dc9d97088852373bfeb5454738cae002b16f452cc35362febfc56023414d05a9

See more details on using hashes here.

Provenance

The following attestation bundles were made for llmkit_sdk-0.1.5.tar.gz:

Publisher: publish-pypi.yml on smigolsmigol/llmkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

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

File metadata

  • Download URL: llmkit_sdk-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 17.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for llmkit_sdk-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 16767ed929ff656c910f44aaf2087f9af974534c8d4ba925ab17b4a721d2df3f
MD5 28af0c8902ad6259ef244931523e9d3b
BLAKE2b-256 4838da50f1e7551d677f6534559f5ef8756f90b3be90f83d1a84bf9702e8beea

See more details on using hashes here.

Provenance

The following attestation bundles were made for llmkit_sdk-0.1.5-py3-none-any.whl:

Publisher: publish-pypi.yml on smigolsmigol/llmkit

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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