Skip to content


Mirascope provides out-of-the-box integration with Langfuse.

How to use Langfuse with Mirascope

from mirascope.langfuse import with_langfuse

with_langfuse is a decorator that can be used on all Mirascope classes to automatically log both Mirascope calls and also all our supported LLM providers.



This is a basic call example but will work with all our call functions, call, stream, call_async, stream_async.

import os
from mirascope.langfuse import with_langfuse
from mirascope.anthropic import AnthropicCall

os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_HOST"] = ""

class BookRecommender(AnthropicCall):
    prompt_template = "Please recommend some {genre} books"

    genre: str

recommender = BookRecommender(genre="fantasy")
response =  # this will automatically get logged with langfuse
#> Here are some recommendations for great fantasy book series: ...

This will give you:

  • A trace around the that captures items like the prompt template, and input/output attributes and more.
  • Human-readable display of the conversation with the agent
  • Details of the response, including the number of tokens used



import os
from mirascope.langfuse import with_langfuse
from mirascope.openai import OpenAIExtractor

os.environ["LANGFUSE_SECRET_KEY"] = "sk-lf-..."
os.environ["LANGFUSE_PUBLIC_KEY"] = "pk-lf-..."
os.environ["LANGFUSE_HOST"] = ""

class TaskDetails(BaseModel):
    description: str
    due_date: str
    priority: Literal["low", "normal", "high"]

class TaskExtractor(OpenAIExtractor[TaskDetails]):
    extract_schema: Type[TaskDetails] = TaskDetails
    prompt_template = """
    Extract the task details from the following task:

    task: str

task = "Submit quarterly report by next Friday. Task is high priority."
task_details = TaskExtractor(
).extract()  # this will be logged automatically with langfuse
assert isinstance(task_details, TaskDetails)
# > description='Submit quarterly report' due_date='next Friday' priority='high'

This will give you the same view as you would get from using langfuse.openai. We will be adding more extraction support for other providers soon.