Observation types: agent, tool, retriever, event
Every observation has a type. as_type marks a function as an agent, a tool, a retriever or one of seven others, and create_event records a moment.
In lesson 3 every observation was a plain span. Types let Langfuse filter and display steps by what they are: all tool calls, all model calls. The desk gets a policy lookup for refund questions, so it has three kinds of step.
POLICIES = {"refunds": "Refunds need approval from a person."}
@observe(name="lookup-order", as_type="tool")
def lookup_order(order_id):
return ORDERS.get(order_id, "not found")
@observe(name="find-policy", as_type="retriever")
def find_policy(topic):
return POLICIES.get(topic, "no policy found")as_type sets the type and name replaces the function's name. A tool does something, such as an order lookup; a retriever only looks something up, such as a policy.
@observe(name="answer-ticket", as_type="agent")
def answer(ticket):
if "refund" in ticket.lower():
langfuse.create_event(name="refund-requested", input=ticket)
return find_policy("refunds")
order = re.search(r"[A-Z]\d{2}", ticket)
if not order:
return "Could you send your order number?"
return f"Order {order.group()}: {lookup_order(order.group())}"The agent decides what to do. A refund ticket records an event, then asks the retriever. create_event records a point in time with no duration, here as a child of the running agent observation.
answer("Where is my order A17?")
answer("I want a refund for B22")
langfuse.flush()
local_langfuse.tree()python typed.pyEach observation shows its type in brackets. The first ticket used the tool; the refund ticket recorded the event and used the retriever. The names read as actions, verb first, which is what Langfuse's guide to good traces recommends: a name identifies the operation, so it never contains an order id or a model name that would split one step into many.
The ten types
| Type | Use it for |
|---|---|
span | Any unit of work with a duration; the default |
event | A single moment, with no duration |
generation | A model call, with model name, token usage and cost (lesson 6) |
agent | A step that decides the application's flow |
tool | A single action, such as an API call |
chain | A link passing context between steps |
retriever | A lookup that changes nothing, such as a search |
evaluator | A function that judges an output (lesson 26) |
embedding | A call that turns text into vectors |
guardrail | A check that protects against unwanted content |
- Mark
find_policyasas_type="guardrail"and see the tree change. - Move
create_eventoutsideanswerand find where the event goes. - Pass
as_type="planner"and read the warning, and the type it falls back to.
This is what real progress feels like.