Skip to content

latent.serve.knowledge_spans

Per-turn knowledge attributes on the agent span.

The agent span is Studio's. Studio consumes the pod's event stream and sets each :class:~latent.events.Metadata event's attributes on the turn's span (target="turn") or on the current step's (target="step"), prefixed agent_studio.meta.. So latent serve stamps a turn by adding Metadata events to the agent's stream (:func:stamp_turn):

  • before the agent's first event: agent.version, knowledge.digest and knowledge.kb (the manifest's knowledge_base), when the pod has them;
  • for each tool call whose tool yielded :func:knowledge_result: knowledge.tool_results.<tool_call_id>.unit_keys and .route, plus knowledge.unit_keys, every unit key the turn's tools returned so far.

"Which unit answered this turn" is then a query on the agent span's knowledge.unit_keys.

Classes

TurnStamp

TurnStamp(attributes: Mapping[str, MetadataValue])

One turn's :func:stamp_turn, keeping the turn-level attributes it emits.

Both served paths wrap the agent's stream with it: /stream sends the events to Studio, and /invoke passes it as the agent's stream_wrapper and returns :attr:turn_metadata, since a buffered InvokeResult keeps no Metadata events. An agent whose invoke ignores stream_wrapper leaves :attr:turn_metadata empty.

Functions

knowledge_result

knowledge_result(unit_keys: Sequence[str], route: str) -> Metadata

The event a knowledge tool yields beside its result.

It names the units the result holds and their delivery route. Unit keys never go into the text the model reads; they travel only here::

@tool
async def lookup(name: str):
    units = index.find(name)
    yield knowledge_result([u.key for u in units], "lookup")
    yield render(units)

stamp_turn

stamp_turn(events: AsyncIterator[AgentEvent], attributes: Mapping[str, MetadataValue]) -> AsyncIterator[AgentEvent]

events with the turn's knowledge attributes added (module docstring).

A :func:knowledge_result event inside a tool call is replaced by its turn-level form, keyed by the call's id: on the step span, two lookups in one step would overwrite each other.

turn_attributes

turn_attributes(version: str | None, knowledge: FetchedKnowledge | None) -> dict[str, MetadataValue]

The attributes :func:stamp_turn sets on every turn of this pod.

Attributes

ROUTE

UNIT_KEYS