Skip to content

feat(run): add UserTraceContext to RunConfig for backend-agnostic trace attribution#278

Open
dev-elie wants to merge 7 commits into
danny-avila:mainfrom
dev-elie:feat/run-user-trace-context
Open

feat(run): add UserTraceContext to RunConfig for backend-agnostic trace attribution#278
dev-elie wants to merge 7 commits into
danny-avila:mainfrom
dev-elie:feat/run-user-trace-context

Conversation

@dev-elie

@dev-elie dev-elie commented Jun 29, 2026

Copy link
Copy Markdown

Summary

Also see: danny-avila/LibreChat#13529 (comment)

Adds UserTraceContext { userId? } to RunConfig so hosts can pass a human-readable user identifier to all configured trace backends (Langfuse, LangSmith, OTel, etc.) from a single place, rather than each backend re-deriving the same mapping.

When RunConfig.user is not set, the SDK falls back to resolving the userId from fields already present in config.configurable — so the improvement works without requiring a host upgrade.

Breaking Change

Traces that previously showed a raw user_id string (e.g. a MongoDB ObjectId) from configurable.user_id will now show the user's email by default. Set LIBRECHAT_TRACE_USER_ID_FIELD=id to preserve the old behaviour.

How to test

  1. Configure a Langfuse (or LangSmith) backend and run an agent via LibreChat.
  2. Open the Langfuse UI — the trace should show a human-readable userId (email by default) instead of a raw ObjectId.
  3. Set LIBRECHAT_TRACE_USER_ID_FIELD=name (or username / id) and restart — verify the correct field appears.
  4. Run unit tests: npm test

Companion PR

danny-avila/LibreChat#14012 — wires buildUserTraceContext(user) into createRun and adds tracing.userIdField to the librechat.yaml config schema

🤖 Generated with Claude Code

dev-elie and others added 3 commits June 29, 2026 19:07
…tribution

- Add UserTraceContext type { userId?, groups? } to RunConfig
- Store user on Run instance
- Use userId (falling back to configurable.user_id) in both agent and title Langfuse traces
- Append group:<name> tags from user.groups in both trace points

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…back

When RunConfig.user is not provided by the host (e.g. older LibreChat
versions that predate the companion PR), fall back to the user identity
already present in config.configurable.user (the LibreChat safe-user
object). This makes user/group attribution work end-to-end without
requiring a LibreChat upgrade.

- Extract resolveConfigurableUserId() helper that reads
  LIBRECHAT_TRACE_USER_ID_FIELD env var (email | username | name | id)
  to pick which field to use as the trace userId; defaults to email.
- Resolve userGroups from configurable.user.groups when
  RunConfig.user.groups is absent.
- Apply the same fallback logic to the title trace.

The explicit RunConfig.user.userId / .groups values always take
precedence over the configurable fallback.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dev-elie dev-elie force-pushed the feat/run-user-trace-context branch from 5abbcbd to f7062ea Compare June 29, 2026 17:08
dev-elie and others added 2 commits June 29, 2026 19:36
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@dev-elie dev-elie marked this pull request as ready for review June 29, 2026 18:37
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…erId

When LIBRECHAT_TRACE_USER_ID_FIELD is unset (default 'email' path) and
no configurable.user object is present, rawUserId (the legacy
configurable.user_id string) was silently dropped, causing userId to be
undefined in propagateAttributes.

All non-identity-field paths now carry rawUserId as the terminal fallback,
restoring backward compat with callers that still pass user_id directly.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant