Skip to content

Observe Agent Workflows with Langfuse

Category AI Solutions Engineering
Status ๐ŸŸก Partially completed
Repo nextwork-observe-agent-workflows-with-langfuse
Original NextWork guide nextwork.ai

Learning project

Practice work following a NextWork guide, separate from my production projects.

๐Ÿ“š NextWork learning project. This is a practice project I built while working through NextWork's Observe Agent Workflows with Langfuse guide. It is part of my Learning portfolio and is fully separate from my production projects.

Category: Learning โ€บ AI Solutions Engineering
Status: ๐ŸŸก Partially completed

Overview

When an agent answers wrongly you need to see which step went wrong. My tracer wraps each step in a span so a run can be read back as a tree with timings, tokens and scores, and failures show up red instead of disappearing into a confident answer.

Summary

I built my own tracing layer instead of Langfuse. A small Python tracer records nested spans (agent, plan, tool, LLM generation) with timing, inputs, outputs, token counts, errors and scores, and saves them as JSONL. A small tool-using agent (price lookups plus a local qwen2.5:1.5b model for the reply) is instrumented with it, and a viewer turns the traces into an HTML summary table and waterfall. Three traced runs are saved, one of which hits an unknown item: the tool span is marked as an error and the trace is scored had_tool_error=1. 2 tests pass (span nesting, scores, error capture and re-raise, viewer). Partial: no Langfuse account or server, the tracer is my own, and Claude Code didn't write it.

The checks behind this are in docs/verification.txt.

Skills & tools

Python, Observability, Tracing, Ollama, pytest

What I built

  • tracer.py: Tracer and context-manager spans (parent and trace ids through contextvars, timing, input/output, tokens, scores, errors), JSONL output and load.
  • agent.py: plan, then price tool calls, then an LLM reply from a local Ollama model, each in its own span, with two scores per trace.
  • viewer.py: static HTML with a per-trace summary (ms, tokens, errors, scores) and a waterfall.
  • docs/traces.jsonl, docs/traces.html, docs/traces.png: the three saved runs.
  • tests/test_tracer.py: nesting, scores, error capture and the viewer, with the model call stubbed out.

How to run

ollama pull qwen2.5:1.5b
python agent.py docs/traces.jsonl
python viewer.py docs/traces.jsonl docs/traces.html
python -m pytest -q tests

Verification

I ran the verification command on 2026-12-03. The full output is in docs/verification.txt.

/workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider tests 2>&1 | tail -1 | grep -q '2 passed' && echo TRACE OK

Notes

The traces caught a real weakness: for an unknown item the agent still says 'Your total is $0.00', which reads as a valid answer. Only the red tool span and the had_tool_error score show something went wrong. The total_in_answer score is 1.0 there too, so that metric alone would have hidden it.

What isn't done, and why

No Langfuse: the tracing, storage and viewer are my own small versions of those ideas. The model is local qwen2.5:1.5b, and Claude Code didn't author it.


Original project: https://nextwork.ai/projects/292ae7a1-55bb-4ab4-911c-ab1f55b31b90 ยท Part of my NextWork Learning portfolio (hub).

Verification output

From docs/verification.txt:

$ /workspace/nextwork-portfolio/.venv/bin/python -m pytest -q -p no:cacheprovider tests 2>&1 | tail -1 | grep -q '2 passed' && echo TRACE OK

TRACE OK

[exit code 0]