Observations
Observations are the building blocks of observability in Langfuse. They are used to track the individual steps of an application.
Observations are either created automatically by one of our integrations or manually by you using the Langfuse SDK.
Observation Types
Langfuse supports different observation types to provide more context to your observations and allow efficient filtering.
Available Types
-
eventis the basic building block. An event is used to track discrete events in a trace. -
spanrepresents durations of units of work in a trace. -
generationlogs generations of AI models incl. prompts, token usage and costs. -
agentdecides on the application flow and can for example use tools with the guidance of a LLM. -
toolrepresents a tool call, for example to a weather API. -
chainis a link between different application steps, like passing context from a retriever to a LLM call. -
retrieverrepresents data retrieval steps, such as a call to a vector store or a database. -
evaluatorrepresents functions that assess relevance/correctness/helpfulness of a LLM’s outputs. -
embeddingis a call to a LLM to generate embeddings and can include model, token usage and costs -
guardrailis 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.
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)