Skip to main content Switch to mobile version inference-tracker 0.11.0 Per-user inference cost for AI apps
pip install inference-tracker==0.11.0 Copy PIP instructions
Per-user inference cost for AI apps.
A small Python library that records each inference call (each call to an AI model) where the app makes it: who it was for, which provider and model, the provider's request id, and how long it took. The provider's bill says how much was spent; the record says who it was for. Prompts, images and other user content are never recorded.
Early work in progress. Python 3.10 or newer, no other packages needed.
pip install "inference-tracker>=0.9.4,<1" Then one line at startup, with the string you get when you make a key (the address, with the key in it):
import inference_tracker inference_tracker.init("https://
The key can only send records, so it may be written in the code of a private repo. Set INFERENCE_TRACKER_DSN to replace the whole string without changing the code (a public repo sets it there and calls init() with nothing). No other variable changes where records go. Without a string, nothing is sent and every call below does nothing: no answer is read, no body parsed. INFERENCE_TRACKER_ENVIRONMENT fills in the environment when it isn't passed. repo is the GitHub repository this code lives in (owner/name): it goes with every batch, so the product shows which repo is recording (INFERENCE_TRACKER_REPO when it isn't passed; without one, the deploy host's service name is sent as a hint). Pass attach=False to wrap clients yourself instead (below).
The older form, init(url=..., key=..., app=...), still works, with INFERENCE_TRACKER_URL, INFERENCE_TRACKER_KEY and INFERENCE_TRACKER_APP winning over it as before.
init() can be called at startup or at the start of every request, from any number of threads at once: called again with the same values, it returns the client already running and does nothing else. So there is no need for a "started" flag of your own (a flag set before init() returns lets a request that comes meanwhile run without a client).
Records are sent in the background every few seconds. Some hosts freeze the process as soon as the response is sent (Cloud Functions, Lambda, Cloud Run by default). There, use init(..., background=False) and context(..., flush=True), which sends the records when the block ends.
That flush runs in your request, so it is kept short:
Only the outermost context(flush=True) block flushes; blocks inside it don't.
It gives up after flush_timeout seconds (1 by default), and never waits on a busy ingest server (a 429, a Retry-After, a retry): records that didn't go stay queued and go with the next flush.
After a flush that didn't get everything through, flushes are skipped for flush_pause seconds (60 by default), so a slow or down server costs one request a second, not every request.
inference_tracker.init(..., background=False, flush_timeout=1.0, flush_pause=60) The background thread, and inference_tracker.shutdown() (run when the process exits), still retry with growing waits.
Say who the work is for where it starts. Every tracked call inside the block gets that user and task:
with inference_tracker.context(user_id=uid, task_id=task.id): ... A call made by another call names that call as its parent, by provider and the provider's request id, the way the bill knows it. The server finds the user and task by walking up the parents, so code that only knows its parent doesn't need them:
with inference_tracker.context(parent_provider="fal", parent_request_id=rid): ... Calls aren't named in the code. Each record carries the app's functions that made it, nearest first (code.functions), which tells apart two places that use the same model; names are given in the product. A name already set with context(call_name="prompt-suggestions") keeps working.
A result read later, somewhere else, keeps the name its call was made with.
Wrapping clients yourself
init() already tracks the clients below. The track() wrappers in each section stay for code written before 0.9.0 and for init(..., attach=False); a wrapped client is still recorded once.
Without init()'s tracking, wrap the client once, then call it as before.
import fal_client import inference_tracker client = inference_tracker.fal.track(fal_client.AsyncClient(key=FAL_KEY)) handle = await client.submit("fal-ai/kling-video/v2.6/pro/motion-control", arguments=args) result = await handle.get() Recorded: submit, subscribe, run, and getting or cancelling a result (handle.get(), handle.cancel(), client.result(...), client.cancel(...)). Everything else (upload, status, stream...) passes through untouched. Tracking never raises an error and never changes the call.
When fal sends the result to a webhook, record the finish in the webhook handler:
inference_tracker.fal.webhook(request_json) # the body fal posts: request_id, status, payload Workflows
Submit a fal workflow with the envelope header, and its result comes back as {output, nodes}, with the request id of each step. Each step is then recorded too, with the run as its parent: from the webhook body, or from the result when it is fetched here (get(), result(), subscribe()). The app reads its output from output.
handle = await client.submit( "workflows/ACME-COMPUTE/prod-myapp-video", arguments=args, webhook_url=hook_url, headers={inference_tracker.fal.WORKFLOW_ENVELOPE_HEADER: "true"}, ) ... inference_tracker.fal.webhook(request_json) # the run's finish, and one record per step When a run fails, the step that failed is recorded with fal's error code. A step that fell back to another model also gets a record for the model that failed.
Our own fal apps make calls of their own. In the endpoint, inside(self) makes the request the app is serving the parent of every tracked call in the block:
@fal.endpoint("/") async def generate(self, input: Input) -> Output: with inference_tracker.fal.inside(self): return await self._inference(input) The request id comes from fal's self.current_request. Without one (the app runs outside fal), the block does nothing.
For calls made with plain HTTP instead of fal_client (or from a subclass of its clients):
http = inference_tracker.fal.track_http(httpx.Client()) # or httpx.AsyncClient, requests.Session, # a fal_client.SyncClient (subclasses too) A submit to queue.fal.run is a "start", fetching the result a "finish", a call to fal.run one record. A submit or fal.run call that gets no answer at all (a timeout, a refused connection) is one record with the exception's class name and no request id: fal may have run it. Other addresses pass through untouched. Use track() or track_http() on a client, not both.
OpenAI and Azure (the openai package)
from openai import OpenAI import inference_tracker client = inference_tracker.openai.track(OpenAI(api_key=...)) response = client.responses.parse(model="gpt-5.4-mini", input=..., text_format=...) Works the same for AsyncOpenAI, AzureOpenAI, and an OpenAI client pointed at Azure AI Foundry (base_url="https://
Also tracked: embeddings.create, moderations.create, streams (stream=True, recorded when the stream ends, with the last chunk's usage; for chat completions pass stream_options={"include_usage": True}, or ModelSettings(include_usage=True) in the Agents SDK, else there is no usage), and copies made with with_options. An OpenAI client pointed at Vertex AI (base_url="https://aiplatform.googleapis.com/v1beta1/projects/
/locations/
A raw client.post(...) is recorded too, on a tracked client: to an images route on any client, and to any inference route on an Azure client, since Azure AI Foundry gives some models a route of their own (FLUX.2-pro at /flux-2-pro). An answer from such a route is read by its shape: image data as images, anything else as a response. Clients init() attached to leave client.post alone: wrap the client with inference_tracker.openai.track(...) for these.
Calls to OpenAI's or Azure's API made without the openai package (httpx.post("https://api.openai.com/v1/decisions", ...), a POST to https://
Google (the google-genai package)
from google import genai import inference_tracker client = inference_tracker.google.track(genai.Client(vertexai=True, project=..., location=...)) response = client.models.generate_content(model="gemini-3.1-flash-image", contents=..., config=...) Tracked, sync and client.aio: models.generate_content, generate_images, edit_image (one record each, with tokens by kind: text and image, in and out, thinking, cached), and Veo's models.generate_videos: a "start" when it is sent and a "finish" when operations.get() returns the operation done, with the seconds of video made. Settings that change the price (resolution, length, audio, image size, aspect ratio, thinking) and the Google Cloud project are noted.
Also models.embed_content (tokens in), and the vertexai package's embeddings: model = inference_tracker.google.track(TextEmbeddingModel.from_pretrained("text-embedding-005")).
LiteLLM (AWS Bedrock, and every other provider it routes to)
inference_tracker.litellm.track() # once at startup, after any `litellm.callbacks = [...]` Every litellm.completion / acompletion, embedding, image and responses call is recorded, streamed or not, including calls the OpenAI Agents SDK's LitellmModel makes (Runner.run, Runner.run_sync). An async call is recorded before it returns; for a sync call, LiteLLM records it on its own thread a moment later, and flush() waits for it, so it is in the flush of the block that made it. Bedrock records carry the model or inference-profile id, the x-amzn-requestid, the region, and input, output, cache-read and cache-write tokens. Don't also pass a client wrapped by inference_tracker.openai.track to LiteLLM (client=), or the call is recorded twice.
For calls to Pruna's API (api.pruna.ai) over plain HTTP, or with Pruna's pruna_client:
http = inference_tracker.pruna.track_http(httpx.AsyncClient()) # or httpx.Client, requests.Session, # a pruna_client.PrunaClient A prediction sent to POST /v1/predictions is a "start" with Pruna's prediction id, and the status answer that says it ended (succeeded, failed, canceled) is the "finish". With Try-Sync, a prediction answered at once is one record, without an id (Pruna gives none). A refused one is one record with Pruna's code (VALIDATION_ERROR...). The model is the Model header (p-video, p-video-2-pro-reference...). Pruna reports no usage, so the record keeps what the price depends on: the settings sent (duration, resolution, mode, draft, fps...) and how many reference images, videos and audio files went with it (counts, never the URLs). Downloads and uploads aren't recorded.
For calls to eachlabs' API (api.eachlabs.ai) over plain HTTP:
http = inference_tracker.eachlabs.track_http(httpx.AsyncClient()) # or httpx.Client, requests.Session A prediction created with POST /v1/prediction is a "start" with eachlabs' prediction id, and the poll answer that says it ended (success, error, cancelled) is the "finish". A refused create is one record with http_
One record per call. A call whose result arrives somewhere else (a queued call, a webhook) gets two: a "start" when it is sent and a "finish" when the result comes back, both with the provider's request id.
Each record has: provider, model, the provider's request id, status and error type (the error's class name or the provider's code, never its message: a provider's text that doesn't look like a code is left out), start time and duration, usage as the provider reports it, and the settings that change the price (copied by name, never the whole input), with where the call was billed: a Google project, an Azure resource, or for a provider that bills by key (OpenAI), the key's hint, its first 8 and last 4 characters, as the provider's own key list shows them; never the key. From the context: user, task, and the call that made this one (parent_provider and parent_request_id, the provider's request id).
When the app stops waiting
When the app cancels a call or a timeout around it runs out (asyncio.CancelledError, asyncio.TimeoutError, fal_client's timeout while polling), the provider may still be running the call, and billing it. So that isn't recorded as a failure:
A call recorded in one record (OpenAI, Google, LiteLLM, fal.run, Modal's remote()) gets status cancelled, with the error's class name as error type.
A call with a "start" and a "finish" (fal's queue, Veo, Modal's spawn(), Pruna) gets no finish: the start stays open, and a later result fetch or webhook finishes it.
A busy or unreachable server is retried with growing waits. Records that still can't be sent, or that the server refuses, are written to the inference_tracker log as one JSON line each. Every batch also tells the server how many records were lost since the last one that got through.
Step-by-step guides, for people and for coding agents:
skills/find-ai-calls/SKILL.md lists every AI call in a repo or a whole folder of repos, and changes nothing.
skills/add-tracking/SKILL.md starts from that list and adds tracking to one app, through a pull request: say who each call is for, wrap the clients, test, connect the bill, and check the numbers.
skills/track-app/SKILL.md (/track-app) does a whole app in one go: runs the two guides above, opens one short pull request per repo, and ends with one table of every call and where it stands. It stops only for what a person has to decide: keys, who a call is for, code that looks dead, library changes.
Released: Dec 4, 2026
Both files in this release have provenance metadata.
View provenance details
Apache-2.0 View SPDX License List
Release files for inference-tracker 0.11.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
Built distribution (wheel)
Total release size: 509.8 kB
Release files / inference_tracker-0.11.0.tar.gz
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Dec 4, 2026.
Security for inference-tracker 0.11.0
Both files in this release have been uploaded with provenance metadata.
With provenance inference_tracker-0.11.0.tar.gz Dec 4, 2026
Attestation for inference_tracker-0.11.0.tar.gz
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.
Release history Release notifications | RSS feed
Dec 4, 2026 2 release files Full provenance
Dec 2, 2026 2 release files Full provenance
Nov 23, 2026 2 release files Full provenance
"PyPI", "Python Package Index", and the blocks logos are registered trademarks of the Python Software Foundation.
© 2026 Python Software Foundation
Deployed from 1274d9b











