Files
nanobot/docs/guides/configure-langfuse-observability.md
T
akinolurandchengyongru 9d2762a4f1 feat(provider): langfuse tracing for codex (#5520)
* feat: trace Codex requests via Langfuse native SDK

Codex has no OpenAI SDK client for Langfuse's usual swap trick, so
log one generation per request through langfuse's native SDK
instead. Tracing failures never affect the returned Codex response.

* docs: note Codex Langfuse tracing support

* fix: trace every Codex request, close every generation

- Move the Langfuse generation inside _send so the compaction
  sub-request gets its own trace too, not just the main request.
- Derive traced model/input from the wire body (post extra_body
  override, post response-item-ID stripping) instead of the
  pre-normalized one.
- Wrap package detection in the same try/except as the SDK call, so
  a broken find_spec() can't turn a valid Codex request into an error.
- Make generation finalization idempotent and cancellation-safe via
  try/except/else/finally, so update() failures or a cancelled
  request no longer leave a generation open.
- Add tests for compaction tracing, update() failures, and
  cancellation.

* docs: fix cli-reference.md langfuse plugin row

* fix: don't trace a rejected compaction as success

- Add a validate hook to _send so a transport-successful compaction
  reply with no usable item traces as an error, not a success.
- Add regression tests for a find_spec() failure, for the traced
  model/input matching the actual wire body (extra_body override,
  ID-stripped replayed items), and for the rejected-compaction case.

* fix: convert LLMUsage to dict for Langfuse usage_details

LLMResponse.usage is now the LLMUsage dataclass upstream, not a
plain dict - Langfuse's usage_details param needs the latter.

* fix: satisfy basedpyright strict on compaction items

* fix(providers): normalize Codex Langfuse cache usage

* fix(providers): migrate Langfuse tracing to SDK 4

---------

Co-authored-by: chengyongru <chengyongru.ai@gmail.com>
2026-10-16 00:44:17 +08:00

2.5 KiB

How to Configure Langfuse Observability for nanobot

nanobot can trace supported OpenAI-compatible provider calls through Langfuse's OpenAI SDK wrapper, and Codex OAuth provider calls through Langfuse's native Python SDK.

What you will build

  • Langfuse installed in the same Python environment as nanobot
  • Langfuse environment variables set before startup
  • one traced nanobot model call

When to use this

Use Langfuse when you need observability for model requests, latency, errors, cost, or prompt behavior during development or production operation.

Install

Complete Quick Start if nanobot is not installed or cannot reply yet. Continue with the same working instance.

Install Langfuse:

nanobot plugins enable langfuse

nanobot uses Langfuse Python SDK 4.7 or later in the 4.x series. Tracing supports Langfuse Cloud and self-hosted Langfuse 3.63.0 or later. The SDK enables real-time ingestion on Langfuse v4 automatically; see Langfuse compatibility.

Minimal working example

Set credentials before starting nanobot:

export LANGFUSE_SECRET_KEY="sk-lf-..."
export LANGFUSE_PUBLIC_KEY="pk-lf-..."
export LANGFUSE_BASE_URL="https://cloud.langfuse.com"
nanobot agent -m "Hello!"

PowerShell:

$env:LANGFUSE_SECRET_KEY = "sk-lf-..."
$env:LANGFUSE_PUBLIC_KEY = "pk-lf-..."
$env:LANGFUSE_BASE_URL = "https://cloud.langfuse.com"
nanobot agent -m "Hello!"

Production notes

  • Langfuse is configured with environment variables, not config.json.
  • Start services from an environment that exports the same variables.
  • Add tracing after the provider works; it should not be the first setup step.
  • Native providers other than Codex OAuth that do not use the OpenAI-compatible client path may not produce Langfuse traces.

Security notes

  • Treat Langfuse projects as observability stores for sensitive prompts and outputs.
  • Use separate projects for personal, staging, and production traffic.
  • Keep Langfuse keys out of committed service files.

Troubleshooting

  • If no traces appear, confirm the service process sees the environment variables.
  • Confirm the provider path is OpenAI-compatible or the Codex OAuth provider.
  • Run one local nanobot agent -m "Hello!" call before debugging service logs.