Skip to content

Identity

ctxflow links turns to a conversation and a user through attributes on the spans (signal S2):

Attribute Meaning
session.id or gen_ai.conversation.id The conversation
user.id The end user, as your agent knows it
ctxflow.turn The number of the turn in the conversation (int ≥ 0)

Without them ctxflow still shows the turns (S1), but cannot group them by conversation, filter by user or attribute tool calls to a user (metric M4). If your framework already sets these attributes, you need nothing else.

import ctxflow_otel
with ctxflow_otel.conversation("conv-42", user_id="u1", session_id="s1", turn=3):
... # every span started here carries the identity

It also works as async with, and can be shared by concurrent tasks. Values must be non-empty, trimmed and at most 256 characters; turn is an integer from 0 to 2^63−1. Invalid values raise.

conversation() puts the values in OpenTelemetry baggage; the SDK’s identity processor copies these baggage entries to every span at start: session.id, gen_ai.conversation.id, user.id (strings) and ctxflow.turn (int). A baggage value longer than 256 characters, or a turn that is not an int in range, is skipped with one warning.

Trust boundary. Identity comes from baggage, so it is as trustworthy as the caller that sent it. Do not use it for authorization. ctxflow shows the user id as received, not verified. It is stored as sent: ctxflow does not pseudonymize it, so send an opaque id if you do not want a real user identifier there.

conversation() attaches the identity to the current context and detaches it on exit. In a generator that yields inside the with, the exit can run in another task than the entry (a server closes a stream when the client disconnects), and then conversation().__exit__ raises LookupError. So do not keep the block open across a yield. Open it around the work that starts the spans, and let that work finish or start as a task inside it: a task copies the context when it is created, so its spans keep the identity after the block has closed.

import asyncio
from collections.abc import AsyncIterator
import ctxflow_otel
async def stream(question: str, conversation_id: str, user_id: str) -> AsyncIterator[str]:
with ctxflow_otel.conversation(conversation_id, user_id=user_id):
task = asyncio.create_task(run_agent(question)) # copies the identity; its spans carry it
# The block is closed before the first yield.
async for event in events_of(task): # your own stream of events from the running task
yield event

A framework that creates the task for you works the same way: call the function that starts the run (for example agent.run(...) of a LlamaIndex workflow, which returns a handler that is already running) inside the with, and read its events outside. If the whole run happens inside one with, with no yield in between, nothing changes.

To carry the identity to your MCP servers, see MCP servers and clients. The conversation, user and turn set here travel with the MCP request; keep baggage in OTEL_PROPAGATORS.