Skip to content

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, model

The 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.

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.

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:

  1. Among the AGENT, invoke_agent and agent.turn spans, the outermost one is the turn (the earliest if there are several at the same level). An AGENT span wins over a CHAIN span, even when the CHAIN span is higher in the tree.
  2. With no such span, the outermost CHAIN span is the turn. This is the case with LlamaIndex, whose FunctionAgent.run span is a CHAIN.
  3. 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.value and output.value are then usually missing: prefer an agent span.
  4. 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_otel
import ctxflow_semconv as sc
from fastapi import FastAPI
from 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 trace
async 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.

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.

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 call span.set_status(StatusCode.ERROR, "..."). ctxflow.tool.status (OK or ERROR) is optional: if present, it is read first.
  • The status description and exception.message / exception.stacktrace are 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.

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.

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.