Groq SDK Integration

Traccia automatically instruments the Groq SDK in Python and TypeScript, including streaming chat completions.

Traccia automatically detects and instruments the Groq SDK (groq in Python, groq-sdk in TypeScript) when it's installed. Call init() and every chat completion is traced, streamed or not, with prompt, completion, token usage, and cost captured on the span. Requires Python SDK 0.1.35 or TypeScript SDK 0.1.22 or later.

1Install Traccia and the Groq SDK

bash
pip install traccia groq

2Initialize Traccia

Initialize Traccia at the start of your application. Groq tracing is auto-enabled when groq (Python) or groq-sdk (TypeScript) is installed:

main.py
python
from traccia import init
# Minimal initialization
init()
# With API key
init(api_key="tr_live_xxxxxxxxxxxx")
# With custom config
init(
endpoint="http://localhost:4318/v1/traces",
agent_id="my-agent",
sample_rate=0.1
)

3Call Groq as Usual

Use client.chat.completions.create() as usual:

groq_call.py
python
from traccia import init
from groq import Groq
init() # Automatically enables Groq tracing
client = Groq() # reads GROQ_API_KEY from the environment
# This call is automatically traced
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": "Write a haiku about recursion"}],
)
print(response.choices[0].message.content)

Traccia monkey-patches the SDK's chat completions create method during init() (sync and async clients in Python), wrapping every call in an llm.groq.chat.completions span. In TypeScript, .withResponse() and .asResponse() keep working, so you can still read rate-limit headers. If you only call .asResponse(), Traccia reads a copy of the body, so the span ends with the completion, token usage, and cost as soon as the response arrives. A streamed call read only through .asResponse() ends when the promise is garbage collected.

What Gets Traced

Every llm.groq.chat.completions span captures:

llm.vendor / llm.model

Set to groq and the model Groq reports it ran. If that differs from the requested model, the requested name is kept in llm.request.model

llm.prompt

Python: the messages joined as role: content lines. TypeScript: the last user message (or the first message if there is none), truncated to 500 characters

llm.groq.messages

The messages as JSON, truncated

llm.completion

The assistant message text, or the joined deltas for streamed calls (truncated to 4,096 characters in Python and 1,000 in TypeScript)

llm.usage.prompt_tokens / completion_tokens / total_tokens

Token usage reported by Groq. prompt_tokens excludes cached prompt tokens, which are recorded as llm.usage.cache_read_tokens. When Groq reports no usage, for example for a stream stopped early, the span records the estimated prompt and completion tokens Traccia settles with instead

llm.usage.source

Where the token counts come from: provider_usage when Groq reported them, estimated when Traccia estimated them. llm.usage.prompt_source and llm.usage.completion_source carry the same value

llm.finish_reason

Why generation stopped, for example stop or length

llm.cost.usd

The cost of the recorded tokens, reported or estimated, priced like the Spend Cap settlement (including a pricing override), when the model is in Traccia's pricing table

llm.streaming

True when streaming is requested

llm.interrupted

Set when Traccia ended the call before it finished: abandoned for a stream dropped unfinished, shutdown for a call still open when tracing stops

Failed calls are recorded on the span with the exception and an error status, then re-raised unchanged.

Streaming

With streaming, the span stays open while you read the stream. When the stream is read to the end, the span ends with the full completion, token usage, and cost:

groq_stream.py
python
stream = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": "Write a haiku about recursion"}],
stream=True,
)
for chunk in stream:
print(chunk.choices[0].delta.content or "", end="")
# The span ends here with llm.completion, token usage, and cost set

Cost and policy settlement are attributed to the agent that made the call, even if the stream is read later or under a different run identity.

Stopping early, aborting, and cancelling

If you stop reading early, abort, or the stream fails, the span records the text received so far. Groq reports token usage only on the last chunk, so a partially read stream has no provider-reported usage. Traccia estimates the tokens instead, from the request and the output received (content, reasoning, and tool call arguments), records them on the span with llm.usage.source set to estimated, and charges your Spend Cap that estimate, so the span’s cost matches the charge. Only a call that fails or is cancelled before any output releases its budget reservation.

A stream dropped without being finished (garbage collected) is stopped, so Groq stops generating it, and is charged its full budget reservation, because how much Groq generated is unknown. Its span still records the estimate of what was received. A call still open when tracing stops is charged its reported usage, or its reservation when that is unknown. It is stopped only if the Spend Cap reserved budget for it; otherwise it keeps running for whoever reads it.

In Python, the span ends when the stream is read to the end, closed, or garbage collected. If you break early, call stream.close() or use a with block, so the span ends and the estimate is settled right away instead of the reservation at garbage collection. An async stream is stopped on its event loop, so not after that loop has stopped. A cancelled async call (asyncio.CancelledError) ends the span with an error.

The stream object

In TypeScript you get groq-sdk’s own Stream, and tee() and toReadableStream() are traced too. In Python you also get groq’s own Stream or AsyncStream, so isinstance checks, iteration, with, close(), and attributes such as response work as before.

Configuration

Groq instrumentation is enabled by default when the SDK is installed and is covered by Traccia’s global patching switch:

python
# Disable all auto-patching (including Groq)
init(enable_patching=False)

Or via environment variable:

bash
export TRACCIA_ENABLE_PATCHING=false

Raw responses

Calls made through client.chat.completions.with_raw_response.create() are traced like any other call: the span records the completion, token usage, and cost, and your .parse() returns the same object. With stream=True, the stream returned by .parse() is traced like any other stream. A with_streaming_response call produces a span and is checked by governance, but you read its body yourself, so Traccia never sees its usage and it is charged its full budget reservation.

Complete Example

movie_recommender.py
python
from traccia import init, span, stop_tracing
from groq import Groq
init()
client = Groq() # reads GROQ_API_KEY from the environment
with span("recommendation_session") as session_span:
session_span.set_attribute("user.preference.genre", "sci-fi")
response = client.chat.completions.create(
model="llama-3.3-70b-versatile",
messages=[{"role": "user", "content": "Recommend 5 sci-fi movies for someone who liked Blade Runner"}],
)
text = response.choices[0].message.content
session_span.set_attribute("recommendations.text_length", len(text))
print(text)
# Flush traces before exit
stop_tracing(flush_timeout=1.0)

Next Steps

© 2026 Traccia.