Skip to content

fix(client): give each graph() run its own run id - #109

Closed
apucacao wants to merge 1 commit into
mainfrom
fix/graph-run-id-per-run-clean
Closed

apucacao wants to merge 1 commit into
mainfrom
fix/graph-run-id-per-run-clean

Conversation

@apucacao

@apucacao apucacao commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Running the same graph() twice with the same context reported both runs under one run id, so separate runs looked like one in the $ld:ai:graph:* events.

graph() caches the built graph per context so it doesn't re-evaluate every node's flag on each call. The run id was created in that build, so it was cached too. Each run now gets a fresh id. Every graph event within a run still shares it, and the flag evaluations stay cached.

Python twin: launchdarkly/python-ai-sdk#132. Source branch: fix/graph-run-id-per-run.

Testing

  • New test in graph.test.ts, for both invoke and stream: two runs on one graph() instance and context get different ids, and all graph events in a run (node, handoff, success) share one. It fails on main.
  • Client, openai-agents, claude-agents, langchain-agents and vercel-agents suites pass. tsc and Biome are clean.

Note

Overview
Fixes graph telemetry so each invoke/stream run gets its own runId. Repeated calls on the same graph() instance and LD context were sharing one id because the built graph (and its track payload) is cached per context.

Each run now clones the cached track data and assigns a fresh runId at execution time. That id is passed through streamRoute / streamNode so all $ld:ai:graph:* events in a run—including handoffs—share it, while flag evaluation stays cached.

Adds a parameterized test for invoke and stream asserting two runs get different ids and events within a run share one id.

Reviewed by Cursor Bugbot for commit 9470b95. Bugbot is set up for automated code reviews on this repo. Configure here.

graph() caches the built graph per context so repeated runs skip
re-evaluating every node's flag. The build also created the graph's
track data, run id included, so every run for the same context
reported the same runId on its $ld:ai:graph:* events.

Each run now copies the cached track data with a fresh runId and passes
it to streamRoute, so the node, handoff and graph-level events of one
run share one id and the next run gets another. The flag evaluations
stay cached.
@apucacao

apucacao commented Oct 2, 2026

Copy link
Copy Markdown
Contributor Author

bugbot run

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ Bugbot reviewed your changes and found no new issues!

Comment @cursor review or bugbot run to trigger another review on this PR

Reviewed by Cursor Bugbot for commit 9470b95. Configure here.

@apucacao apucacao closed this Oct 7, 2026
apucacao added a commit that referenced this pull request Oct 12, 2026
## Summary

Spans from the native graph adapters (`toOpenAIAgents`,
`toClaudeAgents`, `toLangGraph`, `toVercelAgents`) had nothing tying
them to the graph's AI Config, so Monitoring couldn't find them by
config key.

- The `launchdarkly.graph` span now gets `setLdSpanAttributes`, like
every other handler: config key, run id, context keys and the
`feature_flag` event. New helper: `makeGraphTrackData`.
- Graph and node tracking events now carry the environment id.
- Graph-level events (`$ld:ai:graph:invocation_success` and friends) now
use the graph key as `configKey`, matching the span and `graph()`. They
used the root node's key.
- The `graph()` runner's own `launchdarkly.graph` span gets the same
identity.
- Every adapter now ends its graph span in a `finally`. `toLangGraph`
never ended it, and `toOpenAIAgents` / `toClaudeAgents` left it open on
setup errors.
- `makeGraphTrackData` is marked `@internal`.
- A setup error (an agent that was not built, a graph that fails to
compile) now fails the run like a run error does: the span records the
exception with ERROR status, and `$ld:ai:graph:invocation_failure`
fires. Before, the span ended with no status and no failure event.
- Each `graph()` run gets its own run id. The built graph is cached per
context, so runs with the same context used to share one id on the span
and every graph event. This replaces #109.
- `toVercelAgents` no longer sends `$ld:ai:graph:path`, matching the
other adapters. The path stays on the span.

Python twin: launchdarkly/python-ai-sdk#121.

## Testing

- Adapter and client tests cover the span attributes, the event and the
environment id. The LangGraph span-end tests fail on main.
- Staging: [graph run with this
branch](https://ld-stg.launchdarkly.com/projects/default/traces?query=launchdarkly.operation.type%3Dgen_ai+launchdarkly.graph.key%3Dtravel-agent-flow&page=1&relativeTime=last_24_hours&env=production&env=staging&env=alexis-o11y-lab&selected-env=alexis-o11y-lab&related_resource=eyJ0eXBlIjoidHJhY2UiLCJpZCI6IjczYzhjYzU0NWIxNDYxNjg0YWVkMzRjNGU3MDg0MWVkIiwidGltZXN0YW1wIjoiMjAyNi0wOS0zMFQyMzoyNDo1OC45N1oiLCJzcGFuSUQiOiJjNTVmZWFiNjljZjliZDgwIn0%3D).

🤖 Generated with [Claude Code](https://claude.com/claude-code)

<!-- CURSOR_SUMMARY -->
---

> [!NOTE]
> **Overview**
> Native graph runs and the client's `graph()` runner now tag
**`launchdarkly.graph`** spans with the same LaunchDarkly identity as
other handlers via **`setLdSpanAttributes`** and a new
**`makeGraphTrackData`** helper, so AI Config Monitoring can correlate
traces by graph/config key (including **`feature_flag`** span events
with environment id).
> 
> **Telemetry behavior changes:** graph-level `$ld:ai:graph:*` events
and **`invocation_failure`** use the **graph flag key** as `configKey`
(not the root node); **`environmentId`** is added to graph and node
track payloads via **`tryGetEnvironmentId`**. **`graph().stream`** mints
a **fresh `runId` per invocation** even when the built graph is cached.
Setup failures (compile/build) are handled like run errors: span ERROR
status, exception recorded, graph-level failure event, and
**`span.end()`** once in **`finally`** (fixes missing/double span
lifecycle in LangGraph and other adapters). **`toVercelAgents`** stops
emitting **`$ld:ai:graph:path`** as a track event (path remains on the
span only).
> 
> <sup>Reviewed by [Cursor Bugbot](https://cursor.com/bugbot) for commit
e41fe83. Bugbot is set up for automated
code reviews on this repo. Configure
[here](https://www.cursor.com/dashboard/bugbot).</sup>
<!-- /CURSOR_SUMMARY -->

---------

Co-authored-by: Claude Opus 5.5 <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