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

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)
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.4.tar.gz (22.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.4-py3-none-any.whl (17.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: llmkit_sdk-0.1.4.tar.gz
  • Upload date:
  • Size: 22.6 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.4.tar.gz
Algorithm Hash digest
SHA256 fc4bf7ac68b3028eb783b0abf02bd9971db2b90a2ca7f25735fd7430aea5de70
MD5 4ff661a6a1bbd7cdffcd926ddabc67f7
BLAKE2b-256 2b33d7eb4a599d1bb5319775831b97b423d2288b1dbffb8d2a712fd226375c41

See more details on using hashes here.

Provenance

The following attestation bundles were made for llmkit_sdk-0.1.4.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.4-py3-none-any.whl.

File metadata

  • Download URL: llmkit_sdk-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 17.0 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.4-py3-none-any.whl
Algorithm Hash digest
SHA256 1b3c14cf029f785f491dfa0aa53851dfdd3cf786277461038380f2594eed62ac
MD5 eb4784c5fc420d091a73ae640c88ef48
BLAKE2b-256 9c9aeecdfe0566d9260dcef8b7c156b9493a757b7e62f47bf8ca91e32fc84327

See more details on using hashes here.

Provenance

The following attestation bundles were made for llmkit_sdk-0.1.4-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