Skip to main content

Observation Types

Langfuse supports different observation types to provide more context to your observations and allow efficient filtering. (Langfuse calls spans observations; span is also a specific observation type.)

Available Types

  • event is the basic building block. An event is used to track discrete events in a trace.
  • span is the generic observation type for durations of units of work in a trace.
  • generation logs generations of AI models incl. prompts, token usage and costs.
  • agent decides on the application flow and can for example use tools with the guidance of a LLM.
  • tool represents a single action that does something, such as a function or API call (for example a weather API).
  • chain is a link between different application steps, like passing context from a retriever to a LLM call.
  • retriever represents a data-retrieval step that only looks something up rather than changing state, such as a call to a vector store, database, or other knowledge source.
  • evaluator represents functions that assess relevance/correctness/helpfulness of a LLM's outputs.
  • embedding is a call to a LLM to generate embeddings and can include model, token usage and costs
  • guardrail is a component that protects against malicious content or jailbreaks.

How to Use Observation Types

The integrations with agent frameworks automatically set the observation types. For example, marking a method with @tool in langchain will automatically set the Langfuse observation type to tool.

You can also manually set the observation types for your application within the Langfuse SDK. Set the as_type parameter (Python) or asType parameter (TypeScript) to the desired observation type when creating an observation.

Python SDKJavaScript/TypeScript SDK

Note

Observation types require Python SDK version>=3.3.1.

Using @observe decorator:

from langfuse import observe

# Agent workflow
@observe(as_type="agent")
def run_agent_workflow(query):
    # Agent reasoning and tool orchestration
    return process_with_tools(query)

# Tool calls
@observe(as_type="tool")
def call_weather_api(location):
    # External API call
    return weather_service.get_weather(location)

Calling the start_as_current_observation or start_observation method:

from langfuse import get_client

langfuse = get_client()

# Start observation with specific type
with langfuse.start_as_current_observation(
    as_type="embedding",
    name="embedding-generation"
) as obs:
    embeddings = model.encode(["text to embed"])
    obs.update(output=embeddings)

# Start observation with specific type
transform_span = langfuse.start_observation(
    as_type="chain",
    name="transform-text"
)
transformed_text = transform_text(["text to transform"])
transform_span.update(output=transformed_text)