LangfuseLangfuse Python SDK 4.15.4 · Python 3.10+
0%
1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
38 small wins to finish your pathNext lesson

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.

Exampletyped.py, after the setup lines and ORDERS from lesson 3
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.

Exampletyped.py, continued
@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.

Exampletyped.py, continued
answer("Where is my order A17?")
answer("I want a refund for B22")

langfuse.flush()
local_langfuse.tree()
Example
python typed.py

Each 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

TypeUse it for
spanAny unit of work with a duration; the default
eventA single moment, with no duration
generationA model call, with model name, token usage and cost (lesson 6)
agentA step that decides the application's flow
toolA single action, such as an API call
chainA link passing context between steps
retrieverA lookup that changes nothing, such as a search
evaluatorA function that judges an output (lesson 26)
embeddingA call that turns text into vectors
guardrailA check that protects against unwanted content
Try it yourself
  • Mark find_policy as as_type="guardrail" and see the tree change.
  • Move create_event outside answer and 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.