Skip to main content

Python SDK for fin (badboy) - track LLM usage events; import as fin_sdk

Project description

fin-badboy-sdk — Python SDK

Track AI/LLM usage events from Python applications. PyPI: fin-badboy-sdk (import package remains fin_sdk).

Install

From PyPI (package name fin-badboy-sdk).

pip install fin-badboy-sdk

Quick Start

import time
from openai import OpenAI
from fin_sdk import FinClient

client = OpenAI()
fin = FinClient()

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

fin.track_response(
    response,
    provider="openai",
    feature_id="chat",
    latency_ms=(time.time() - start) * 1000,
)

Product environment (development | production) is not passed into track_response—it is set on the server from your API key (Epic 21). Use a key that matches the fin deployment URL.

Configuration

Set environment variables (read at construction time):

export FIN_INGEST_URL=https://events.badboy.dev
export FIN_API_KEY=sk-your-api-key

For experiment resolution (resolve_model / experiment), also set FIN_API_URL (api-nest base URL), for example https://api.badboy.dev.

Or pass explicitly:

fin = FinClient(
    ingest_url="https://events.badboy.dev",
    api_key="sk-your-api-key",
)

For local development, set ingest_url to your local events service base URL (no trailing slash).

API

track_response(response=None, *, provider, feature_id, ...)

Build and POST a usage event from a provider response. response is positional-or-keyword; all other parameters are keyword-only.

Parameter Type Required Description
response Any No Raw LLM response; SDK auto-extracts usage + model
provider str Yes Use a key from Supported Providers below (aligned with Fin’s global pricing model for server-side cost), or any string with explicit usage / model
feature_id str Yes Workflow/feature name
model str No On the success path, when set, overrides the model extracted from response for ingest (use a canonical id when the provider returns a dated snapshot on response.model). When omitted, the extracted model is used.
usage dict No {"input_tokens": N, "output_tokens": N, "total_tokens": N} (snake_case; converted to camelCase internally)
latency_ms float No End-to-end latency in milliseconds
error bool No True on error path (zeroes usage)
tenant_id str No Your end-user/customer ID
plan_tier str No e.g. "free", "pro"
outcome str No e.g. "success", "refusal"
cost float No Client-supplied cost in USD
idempotency_key str No De-duplicate retries

Cost: pass cost when you trust your own figure (e.g. provider billing). Omit it to let Fin derive an estimate from its global pricing model using provider, model, usage, and event time. The same applies to track() when you build the full camelCase payload.

For OpenAI snapshot ids on response.model, pass model= with your canonical request name, or rely on server-side normalization in the events service (see project docs/getting-started.md, “Model id for pricing”).

track(payload, *, idempotency_key=None)

Post a pre-built event dict. All keys must be camelCase. Missing required fields are logged and the event is silently skipped.

Epic 21: track_response does not send environment; the events service sets it from the API key. Required fields for track() match the public ingest schema (featureId, provider, model, usage, timestamp, etc.) — not environment.

Supported Providers

These keys align with Fin’s global pricing model (same provider ids as server-side cost lookup).

Provider key Extraction
"openai" usage.prompt_tokensinputTokens, usage.completion_tokensoutputTokens, usage.total_tokenstotalTokens; model
"groq" Same as "openai"
"azure_openai" Same as "openai"
"mistral" Same as "openai" (Mistral chat API is OpenAI-compatible)
"anthropic" usage.input_tokensinputTokens, usage.output_tokensoutputTokens; totalTokens derived as input + output; model
"google" usageMetadata.promptTokenCount, .candidatesTokenCount, .totalTokenCount (snake_case variants supported); model from model, modelVersion, or model_version

Other provider strings still work: pass usage and model explicitly on track_response, or use track() with a full camelCase payload.

Error Handling

The SDK never raises on ingest failure. HTTP errors and network issues are logged via logging.getLogger("fin_sdk") with a [fin-sdk] ingest failed: prefix. Your application flow is never disrupted.

Full Documentation

See docs/getting-started.md for the complete integration guide.

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

fin_badboy_sdk-0.2.2.tar.gz (14.3 kB view details)

Uploaded Source

Built Distribution

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

fin_badboy_sdk-0.2.2-py3-none-any.whl (11.0 kB view details)

Uploaded Python 3

File details

Details for the file fin_badboy_sdk-0.2.2.tar.gz.

File metadata

  • Download URL: fin_badboy_sdk-0.2.2.tar.gz
  • Upload date:
  • Size: 14.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fin_badboy_sdk-0.2.2.tar.gz
Algorithm Hash digest
SHA256 dd0239a64e45b1fad3a4abd4810ac9bc5e47140f3bbc8fc67977f9acb513fa99
MD5 c0ae3db6d2a5d6383f48c2716c9b3183
BLAKE2b-256 79b4e4b0d81d5a5c021724e00037a91e59c3611e43481f7d7f5959843717e227

See more details on using hashes here.

File details

Details for the file fin_badboy_sdk-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: fin_badboy_sdk-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 11.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for fin_badboy_sdk-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0e62f7329f6a020a9b57ce387f00b70d20f755be74b9c7ee455d612929740ce2
MD5 c0f268565be49aaf4254429b9e0a0da7
BLAKE2b-256 790e53208cd7c9d405af7a03754ee1f6e9bae0ae77f7bccb52652583b20b4262

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