Skip to content

feat: Graph tracking refactor — ManagedAgentGraph drives tracking for new runner shape#154

Draft
jsonbailey wants to merge 19 commits intomainfrom
jb/aic-2174/graph-tracking-refactor
Draft

feat: Graph tracking refactor — ManagedAgentGraph drives tracking for new runner shape#154
jsonbailey wants to merge 19 commits intomainfrom
jb/aic-2174/graph-tracking-refactor

Conversation

@jsonbailey
Copy link
Copy Markdown
Contributor

Summary

Moves graph-level LaunchDarkly tracking from inside the runner implementations to the ManagedAgentGraph managed layer.

  • ManagedAgentGraph.run() now detects runner result type via isinstance:
    • AgentGraphRunnerResult (new shape): managed layer drives all graph-level tracking (path, duration, success/failure, total tokens) from result.metrics via the graph tracker
    • AgentGraphResult (legacy shape): tracking already occurred inside the runner; managed layer wraps the result without additional tracking
  • ManagedAgentGraph now accepts an optional graph: AgentGraphDefinition parameter used to create the graph tracker
  • LDAIClient.create_agent_graph() passes the resolved graph definition as graph=graph

Deliberate bridge pattern: The legacy detection branch exists because PR 11-openai and PR 11-langchain have not yet migrated their runners to return AgentGraphRunnerResult. Once both runners are migrated, the legacy AgentGraphResult branch becomes dead code and will be removed in PR 11-langchain's cleanup commit.

Depends on

Test plan

  • All existing tests pass (uv run pytest packages/sdk/server-ai/tests/)
  • New tests: test_managed_agent_graph_run_handles_new_shape, test_managed_agent_graph_new_shape_drives_tracking, test_managed_agent_graph_new_shape_no_graph_skips_tracking
  • Legacy tests continue to pass (AgentGraphResult path unchanged)

🤖 Generated with Claude Code

@jsonbailey jsonbailey force-pushed the jb/aic-2388/enrich-metrics branch from cbe2eda to 4f12618 Compare April 29, 2026 13:15
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from fcbcb18 to 9286d53 Compare April 29, 2026 13:15
@jsonbailey jsonbailey force-pushed the jb/aic-2388/enrich-metrics branch from 4f12618 to 351f249 Compare April 29, 2026 13:19
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from 9286d53 to 72fc13e Compare April 29, 2026 13:19
@jsonbailey jsonbailey force-pushed the jb/aic-2388/enrich-metrics branch from 351f249 to ef03235 Compare April 29, 2026 13:22
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from 72fc13e to bde4f09 Compare April 29, 2026 13:22
@jsonbailey jsonbailey force-pushed the jb/aic-2388/enrich-metrics branch from ef03235 to e5ecc6a Compare April 29, 2026 13:52
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from bde4f09 to c376011 Compare April 29, 2026 13:52
@jsonbailey jsonbailey force-pushed the jb/aic-2388/enrich-metrics branch from e5ecc6a to 1636446 Compare April 29, 2026 13:57
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from c376011 to 7f67e4f Compare April 29, 2026 13:57
@jsonbailey jsonbailey force-pushed the jb/aic-2388/enrich-metrics branch from 1636446 to 0fae2df Compare April 29, 2026 14:38
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from 7f67e4f to a89c6a2 Compare April 29, 2026 14:38
jsonbailey and others added 2 commits April 29, 2026 11:25
@jsonbailey jsonbailey force-pushed the jb/aic-2388/enrich-metrics branch from 0fae2df to a468a7f Compare April 29, 2026 16:33
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from a89c6a2 to c69a9ff Compare April 29, 2026 16:33
jsonbailey and others added 9 commits April 29, 2026 11:49
The new track_tool_calls method at line 413 (with summary storage and
dedup guard) was being shadowed by the older method at line 559 (which
only fired per-tool events). Merge them into a single method that both
stores to the summary and fires per-tool events.
Previously, metrics_extractor(result) was called twice — once in the
public track_metrics_of/track_metrics_of_async to read duration_ms,
and again inside _track_from_metrics_extractor to track success,
tokens, and tool calls. Extract metrics once in the public method and
pass the resulting metrics + elapsed_ms into the private helper, which
now also handles the duration tracking.
ManagedModel and ManagedAgent now require a Runner. The compat shims
(_invoke_runner, isinstance(result, RunnerResult) branches, Union
type annotations) are removed; result handling is direct on
RunnerResult fields.

The deprecated ManagedModel.invoke() is preserved for backwards compat
but now delegates to run() and adapts the ManagedResult into the legacy
ModelResponse shape.

ModelRunner and AgentRunner protocol definitions remain in place so
downstream provider packages that import them continue to work.
- Drop the inconsistent 'if metrics else None' guard on reported_ms;
  the next line already dereferences metrics.success unconditionally.
- Use 'is not None' for tool_calls so an explicit empty list still
  triggers tracking (preserves the distinction between 'not tracked'
  and 'tracked with no calls').
Drop the deprecated invoke() method from the managed layer along with
its dedicated test class and the warnings/LDAIMetrics/ModelResponse
imports that were only needed by it. Type definitions in providers/
remain so downstream provider packages keep building.
…unner]

The factory's downstream consumers (ManagedModel, ManagedAgent) now
take Runner; aligning the factory's return types lets us drop the
type: ignore comments at the ManagedModel/ManagedAgent call sites.
Provider package PRs will update their concrete implementations to
match.

Judge still takes ModelRunner, so its call site picks up the
type: ignore[arg-type] in its place — that's resolved later in the
cleanup PR when Judge migrates to Runner.
Move the metrics_extractor call inside _track_from_metrics_extractor
so extraction errors are caught and logged without bubbling up. When
extraction fails or returns None, only the wall-clock duration is
tracked — success/error is left untouched since the underlying model
call itself succeeded.

Also tighten the tool_calls check to access metrics.tool_calls
directly, mirroring how metrics.usage is accessed.
- Judge now accepts Runner instead of ModelRunner
- evaluate() calls runner.run(output_type=...) instead of invoke_structured_model
- response.parsed replaces StructuredResponse.data; None guard added
- evaluate_messages() accepts RunnerResult instead of ModelResponse
- Tests updated to use RunnerResult and mock_runner.run

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from c69a9ff to 14cfa92 Compare April 30, 2026 14:03
@jsonbailey jsonbailey changed the base branch from jb/aic-2388/enrich-metrics to jb/aic-2388/managed-graph-result April 30, 2026 14:09
@jsonbailey jsonbailey force-pushed the jb/aic-2388/managed-graph-result branch from 0a1ee15 to 72f5a37 Compare April 30, 2026 14:22
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from 14cfa92 to 1ed1a44 Compare April 30, 2026 14:23
jsonbailey and others added 6 commits April 30, 2026 09:41
…nnerResult

- OpenAIModelRunner.run() implements the unified Runner protocol; returns RunnerResult
  with content, metrics (LDAIMetrics), raw, and parsed fields. Structured output is
  supported via the output_type parameter.
- OpenAIAgentRunner.run() updated to return RunnerResult; populates tool_calls in
  LDAIMetrics from observed openai-agents ToolCallItems.
- Legacy invoke_model() and invoke_structured_model() retained as deprecated adapters
  that delegate to run() and wrap results into ModelResponse / StructuredResponse for
  backward compatibility.

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

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

- LangChainModelRunner.run() implements the unified Runner protocol; returns RunnerResult
  with content, metrics (LDAIMetrics), raw, and parsed fields. Structured output is
  supported via the output_type parameter.
- LangChainAgentRunner.run() updated to return RunnerResult; populates tool_calls in
  LDAIMetrics from observed tool_calls in message responses.
- Legacy invoke_model() and invoke_structured_model() retained as deprecated adapters
  that delegate to run() and wrap results into ModelResponse / StructuredResponse for
  backward compatibility.

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

- LangChainModelRunner: replaces invoke_model/invoke_structured_model with
  run(input, output_type=None); returns RunnerResult
- LangChainAgentRunner: replaces AgentResult with RunnerResult; run()
  signature gains optional output_type parameter
- Tests updated to call run() and assert result.content / result.parsed

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

- Add GraphMetrics dataclass (runner-layer return type for graph runs)
- Add GraphMetricSummary dataclass (managed-layer metrics, analogous to
  LDAIMetricSummary for single-model invocations)
- Add ManagedGraphResult dataclass (managed-layer return type from ManagedAgentGraph)
- Add AgentGraphRunnerResult dataclass (future runner return type, no evaluations field)
- ManagedAgentGraph.run() now returns ManagedGraphResult with GraphMetricSummary
  built from the runner's AgentGraphResult metrics
- Export all new types from ldai package

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jsonbailey jsonbailey force-pushed the jb/aic-2388/managed-graph-result branch from 72f5a37 to 8e60f79 Compare April 30, 2026 14:44
… new runner shape

ManagedAgentGraph.run() now detects the runner result type and dispatches
accordingly:
- AgentGraphRunnerResult (new shape): managed layer drives all graph-level
  tracking from result.metrics (path, duration, success/failure, total tokens)
  via the graph tracker. Node-level tracking from node_metrics will be wired
  once runners populate that field (PR 11-openai/langchain).
- AgentGraphResult (legacy shape): tracking already occurred inside the runner;
  managed layer wraps result without additional tracking.

ManagedAgentGraph now accepts an optional graph parameter (AgentGraphDefinition)
used to create the graph tracker. LDAIClient.create_agent_graph() passes the
resolved graph definition. This is a deliberate bridge pattern: the legacy
detection branch will be removed once both runners are migrated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@jsonbailey jsonbailey force-pushed the jb/aic-2174/graph-tracking-refactor branch from 1ed1a44 to f016b0d Compare April 30, 2026 14:46
@jsonbailey jsonbailey force-pushed the jb/aic-2388/managed-graph-result branch 3 times, most recently from f9e947b to 75172d8 Compare May 1, 2026 21:37
Base automatically changed from jb/aic-2388/managed-graph-result to main May 1, 2026 22:07
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