Skip to navigation

Instrument an LLM Call

View as MarkdownOpen in Claude

Use this guide when you own the model-provider callback and want NeMo Relay to emit lifecycle events, apply LLM middleware, and preserve the active agent scope around the call.

What You Build

You will wrap one existing LLM provider invocation with the managed LLM execution API. The result is an LLM call that:

  • Receives an LLM request object such as LLMRequest in Python or LlmRequest in Node.js and Rust.
  • Runs LLM request intercepts, guardrails, execution intercepts, and response guardrails.
  • Emits LLM start and LLM end events.
  • Records model metadata for observability and trajectory export.
  • Keeps the LLM span attached to the current agent or request scope.
  • Returns the original provider result to the application.
1

Prepare

Complete one binding Quick Start guide first:

The example creates one agent-run scope to group the LLM call. You can use it without first completing the scopes guide. For additional grouping and optional checkpoints, refer to Adding Scopes and Marks.

The request and response payloads must be JSON-compatible. If your provider SDK uses clients, streams, callbacks, or other opaque objects, keep those objects in the provider callback and pass only a serializable request projection into NeMo Relay.

2

Instrument

Use the Execute helper for the provider invocation your application owns.

Choose a stable provider name and create or inherit a scope for the current agent run, request, or workflow. Register a temporary subscriber, build an LLM request object with provider headers and content, then replace the direct provider invocation with Execute. Pass the active scope handle and a stable model_name. The highlighted calls below show that replacement in each binding.

Add a response codec afterward when subscribers or exporters need normalized response usage, tool calls, or cost annotations.

3

Run

Run the complete example for your binding in the project from its Quick Start. Each example wraps a local callback and prints emitted events. The callback returns a response containing hello and the input messages. Replace its body with your provider SDK invocation when connecting a model provider.

Save the following code as llm_call.py in your Quick Start project:

llm_call.py
# SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
import asyncio
import nemo_relay
def log_event(event: nemo_relay.Event) -> None:
print(f"{event.kind} {event.name}")
async def call_provider(request: nemo_relay.LLMRequest) -> dict[str, object]:
return {
"text": "hello",
"messages": request.content["messages"],
}
async def main() -> nemo_relay.Json:
nemo_relay.subscribers.register("llm-check", log_event)
try:
with nemo_relay.scope.scope("agent-run", nemo_relay.ScopeType.Agent) as handle:
request = nemo_relay.LLMRequest(
{},
{"messages": [{"role": "user", "content": "hello"}]},
)
result = await nemo_relay.llm.execute(
"demo-provider",
request,
call_provider,
handle=handle,
model_name="demo-model",
)
print(result)
finally:
await nemo_relay.subscribers.flush_async()
nemo_relay.subscribers.deregister("llm-check")
return result
if __name__ == "__main__":
asyncio.run(main())

API reference: nemo_relay.llm.execute.

Run it from the project directory:

uv run python llm_call.py
4

Verify

The subscriber prints these event lines for the local callback:

Expected event lines
scope agent-run
scope demo-provider
scope demo-provider
scope agent-run

The returned payload contains:

Expected result
{
"text": "hello",
"messages": [
{
"role": "user",
"content": "hello"
}
]
}

Printed object formatting differs across bindings. Subscriber delivery is asynchronous, so check the event sequence separately from where the result is printed.

Check both behavior and instrumentation:

  • The result matches the local callback’s response: text: "hello" and the input messages.
  • The subscriber prints agent-run twice, for scope start and end.
  • The subscriber prints demo-provider twice, for LLM start and end.
Flush Subscriber Delivery

Native subscriber delivery is asynchronous. From an asyncio task, await nemo_relay.subscribers.flush_async(). In Node.js, await flushSubscribers() before validating printed output. If a subscriber callback emits a native event, await flushSubscribers() again to observe that event.

This example verifies Relay execution around a local function. It does not make an external model request.

If the result appears without LLM events, check that the subscriber is registered, subscriber delivery has been flushed, and the call uses llm.execute, llmCallExecute, or llm_call_execute.

Inspect LLM Event Payloads

  • LLM start input contains the request after request intercepts and sanitize-request guardrails. Each owning agent scope starts fresh, and a compaction mark refreshes it. The first subsequent LLM start retains complete annotation history. Later starts retain system instructions, the latest user message, and every following assistant or tool message. When a request codec supplies an annotation, the emitted event input uses the same projection. If that event-only encoding fails, Relay retains the complete event input and annotation. The provider execution request remains unchanged in either case.
  • Freshness belongs to agent scopes, including the implicit root. Non-agent scopes, including function and custom scopes used to model requests, inherit their nearest agent’s state and do not start a new freshness budget.
  • LLM end output contains the provider response after response guardrails.
  • The LLM event includes the normalized model_name when you provide one.

Provider Metadata and Pricing

For every managed LLM request, Relay automatically propagates agent lineage to Dynamo using x-dynamo-session-id and, when a parent agent scope is active, x-dynamo-parent-session-id. Relay takes these IDs from the most recent explicit Agent scopes in the active scope stack and ignores the implicit root scope. It uses harness session metadata when available and application scope names otherwise. No plugin or configuration is required.

Relay also adds a W3C traceparent header to every managed buffered or streaming LLM provider request. The value is derived from the same Relay IDs used by OpenTelemetry export, so downstream HTTP spans can remain aligned with the Relay LLM span. Any existing traceparent values are replaced by the runtime-owned value. Use capture_traceparent() when an application needs to inject the current context into an arbitrary HTTP payload.

If you want Relay to add cost estimates, initialize the built-in pricing plugin before the LLM call and attach a response codec that decodes model and token usage from the provider response. Provider- or framework-reported cost is preserved when present. Otherwise Relay estimates cost only when a configured model pricing source matches the response model and usage fields. For catalog setup and embedded plugin examples, refer to Provider Response Codecs and Model Pricing.

When those conditions match, LLM end events include annotated_response.usage.cost.

Production Checklist

Use this checklist before running the pattern in production traffic.

  • Keep provider names stable. Subscribers and exporters use names for filtering and dashboards.
  • Pass model_name separately when the model should be easy to filter or export.
  • Keep request and response payloads JSON-compatible.
  • Keep SDK clients and transport objects inside the provider callback.
  • Use request codecs when request intercepts or request-side middleware need normalized provider request semantics.
  • Use response codecs when LLM end events, subscribers, or exporters need normalized provider response annotations.
  • Use response codecs and the pricing plugin when exporters need cost estimates from model pricing.
  • Use sanitize guardrails before exporting prompts or model responses in production.

Common Issues

Check these symptoms first when the workflow does not behave as expected.

No LLM Events Appear

Confirm the call uses an Execute helper, the subscriber is registered before the call, and subscriber delivery is flushed before you inspect the output.

The LLM Appears Outside the Agent Scope

Pass the current scope handle into the managed execute helper.

Middleware Sees Provider-Specific Shapes

Add a codec so request intercepts can work with normalized annotated data.

Sensitive Prompt Data Appears in Traces

Add LLM sanitize-request and sanitize-response guardrails before registering production exporters.

Next Steps

Use these links to continue from this workflow into the next related task.