Agent turns, models and tools
ctxflow reads standard OpenTelemetry spans. To get every view, one user turn should look like this: one trace per turn, with a root agent span and, under it, retrieval, tool and model spans.
agent.turn (root: openinference.span.kind=AGENT, output.value = the answer)├── retrieval (RETRIEVER: retrieval.documents.N.document.*) → Retrieval├── execute_tool <name> (TOOL: tool.name, status ERROR on failure) → Tools└── chat <model> (gen_ai.operation.name, gen_ai.usage.*) → tokens, modelThe turn span need not be the first span of the trace: see How ctxflow finds the turn. A complete runnable example is on the Retrieval page. If you use LangChain, LlamaIndex or another instrumented framework, these spans are created for you: see Integrations.
The turn (signal S1)
Section titled “The turn (signal S1)”A turn is one trace. ctxflow recognizes these spans as the turn:
| Span | Convention |
|---|---|
agent.turn with openinference.span.kind=AGENT |
ctxflow SDK constant ctxflow_semconv.AGENT_TURN_SPAN |
invoke_agent (gen_ai.operation.name=invoke_agent) |
OTel GenAI |
A span with openinference.span.kind AGENT or CHAIN |
OpenInference |
Set the answer as plain text in output.value on the turn span, and the question in input.value: the turn detail shows them as the answer and the prompt. When the turn span has no output, the output of the last model span is used.
The turn’s status is its span status. A failed tool does not fail the turn; set the turn span’s status to ERROR if you want the turn itself to read as failed.
How ctxflow finds the turn
Section titled “How ctxflow finds the turn”The agent span does not have to be the root of the trace. A web framework usually starts the trace with its own span (for example POST /api/chat from FastAPI instrumentation) and your agent runs below it. ctxflow looks for the turn among all the spans of the trace:
- Among the
AGENT,invoke_agentandagent.turnspans, the outermost one is the turn (the earliest if there are several at the same level). AnAGENTspan wins over aCHAINspan, even when theCHAINspan is higher in the tree. - With no such span, the outermost
CHAINspan is the turn. This is the case with LlamaIndex, whoseFunctionAgent.runspan is aCHAIN. - With none of them, but with tool, model or retrieval spans, ctxflow takes the first span of the trace without a parent (the HTTP span) as the turn. Its
input.valueandoutput.valueare then usually missing: prefer an agent span. - A trace with no agent span and no tool, model or retrieval span, such as an index build, has no turn.
Spans of other conventions that describe the same time range are merged into that turn. Only one turn per trace is built: a trace that handles two user questions shows one turn. Keep one user turn per trace.
You do not need to create your own root span. The HTTP span comes from opentelemetry-instrumentation-fastapi (install it: ctxflow-instrument then starts it). This is a FastAPI handler with the HTTP span first and the agent span below it; run_agent stands for your own retrieval, tool and model calls:
import ctxflow_otelimport ctxflow_semconv as scfrom fastapi import FastAPIfrom opentelemetry import trace
app = FastAPI() # run: ctxflow-instrument uvicorn app:app
async def run_agent(question: str) -> str: """Your agent: retrieval, tool and model calls. Spans started here are children of the turn.""" ...tracer = trace.get_tracer("my-agent")
@app.post("/api/chat") # the FastAPI instrumentation makes the HTTP span: the first span of the traceasync def chat(question: str, conversation_id: str, user_id: str) -> dict[str, str]: with ctxflow_otel.conversation(conversation_id, user_id=user_id): with tracer.start_as_current_span(sc.AGENT_TURN_SPAN) as turn: # the turn: child of the HTTP span turn.set_attribute("openinference.span.kind", "AGENT") turn.set_attribute("input.value", question) answer = await run_agent(question) # your retrieval, tool and model calls turn.set_attribute("output.value", answer) return {"answer": answer}With LlamaIndex the agent span comes from the instrumentation (FunctionAgent.run, a CHAIN): you add no span, only conversation(...) around the call. Set input.value and output.value yourself only if you need the answer in the turn detail and the instrumentation did not write them.
Streaming handlers need care with conversation(): see Identity.
Model calls: tokens and model
Section titled “Model calls: tokens and model”On each model span set:
| Attribute | Example |
|---|---|
gen_ai.operation.name |
chat, text_completion or generate_content. This is how ctxflow recognizes a model call. Without it the token attributes are ignored and the turn shows tokens and model as “n/a”. |
gen_ai.request.model (or gen_ai.response.model) |
gpt-4.1 |
gen_ai.usage.input_tokens, gen_ai.usage.output_tokens |
120, 18 |
Prompts and completions (gen_ai.input.messages, gen_ai.output.messages, llm.input_messages.*…) are content: they are sent and stored by default; the project’s Store text option decides what the core keeps.
Tool calls (signal S3)
Section titled “Tool calls (signal S3)”A tool span is execute_tool <name> (OTel GenAI) or a span with openinference.span.kind=TOOL, with tool.name.
- A failure is the span status ERROR. An exception that leaves
with tracer.start_as_current_span(...)sets it for you; otherwise callspan.set_status(StatusCode.ERROR, "...").ctxflow.tool.status(OKorERROR) is optional: if present, it is read first. - The status description and
exception.message/exception.stacktraceare content: sent, and stored like any other text. - Input and result (
input.value,output.value,gen_ai.tool.call.arguments,gen_ai.tool.call.result) are shown in the turn detail when text is stored.
Both sides of a call
Section titled “Both sides of a call”When the tool runs in another process (an MCP server), the agent’s span is the client side and the server’s span is the server side. With trace context propagated (see MCP servers and clients) they share the trace, and ctxflow pairs them into one logical call. Metric M5 checks that every remote client call has its server twin, and M4 that every server call names a user and a conversation.
Retrieval through a tool
Section titled “Retrieval through a tool”A tool that does retrieval (a search tool of an MCP server, a search_documents function tool) is a tool call. The chunks are listed only if a retrieval span carries them: write one around the search (a manual retrieval span), or rely on the retriever span of your instrumentation. A retrieval span nested in the tool call makes the call a retrieval call; you mark nothing. For a tool of an MCP server whose retrieval runs in the server, see Retrieval in another process.