feat(sdk): TokenJamClient LiteLLM Integration#61
Merged
Conversation
Adds tokenjam.sdk.TokenJamClient — a thin HTTP client that POSTs a single
LiteLLM call as an OTLP JSON span to a running tj serve. Designed to be
embedded in foreign codebases that cannot rely on the in-process OTel
TracerProvider — most notably the upstream BerriAI/litellm named-callback
machinery (litellm.success_callback = ["tokenjam"]).
Public surface:
from tokenjam.sdk import TokenJamClient
c = TokenJamClient(endpoint="http://localhost:7391", ingest_secret=...)
c.emit_litellm_span(kwargs, response_obj, start_time, end_time, success)
The method translates LiteLLM's callback payload (kwargs/response_obj/times)
into an OTLP JSON span — provider, model, input/output tokens, cache tokens,
cost (from kwargs["response_cost"] or response._hidden_params), and optional
agent/session tags from kwargs["metadata"]["tj_agent_id" | "tj_session_id"].
Non-blocking by design: all errors are logged at debug and the event is
dropped — never propagates an exception into the caller's request path.
Bumps version to 0.2.2.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds
tokenjam.sdk.TokenJamClient— a thin HTTP client that POSTs a single LiteLLM call as an OTLP JSON span to a runningtj serve. Unblocks the upstream BerriAI/litellm named-callback PR (litellm.success_callback = [\"tokenjam\"]), which depends on a public surface that doesn't require running inside a tokenjam-aware app.Bumps version to 0.2.2.
Public surface
```python
from tokenjam.sdk import TokenJamClient
c = TokenJamClient(
endpoint="http://localhost:7391\", # default
ingest_secret="...", # optional Bearer auth
)
c.emit_litellm_span(
kwargs={"model": "openai/gpt-4o-mini", "metadata": {"tj_agent_id": "my-agent"}},
response_obj=response,
start_time=start,
end_time=end,
success=True,
)
```
What gets captured
gen_ai.request.model(provider prefix stripped — `openai/gpt-4o-mini` → `gpt-4o-mini`)gen_ai.provider.name(fromresponse._hidden_params['custom_llm_provider'], falling back to the model prefix)gen_ai.usage.input_tokens/output_tokens/cache_read_tokens/cache_creation_tokenstokenjam.cost_usd(fromkwargs['response_cost']orresponse._hidden_params['response_cost'])gen_ai.agent.id/gen_ai.conversation.id(fromkwargs['metadata']['tj_agent_id' | 'tj_session_id'])Design notes
debug— the method never raises into the caller's request path. Matches the idiom expected by LiteLLM, Langfuse, Helicone, etc.bootstrap/ensure_initialised()or the OTel TracerProvider, so it can be safely instantiated from inside LiteLLM without side effects on the host process./api/v1/spanspath.patch_litellm()remains the preferred path — it produces equivalent spans via the OTel pipeline.Files
tokenjam/sdk/client.py(new, 230 lines) —TokenJamClient+ pure-function payload builderstokenjam/sdk/__init__.py— re-exportTokenJamClienttests/unit/test_litellm_client.py(new, 10 tests) — covers payload shape, provider/model fallbacks, dict vs pydantic usage, error status, auth header, swallowed connection / build errorspyproject.toml,sdk-ts/package.json,sdk-ts/package-lock.json— bump to 0.2.2Test plan
🤖 Generated with Claude Code