> This page is for NeMo Relay, version v1.0.0.
> For other versions, use one of these documentation indexes:
> - Latest (v0.11.2) (default): https://docs.nvidia.com/nemo/relay/latest/llms.txt
> - dev: https://docs.nvidia.com/nemo/relay/dev/llms.txt
> - v1.0.0: https://docs.nvidia.com/nemo/relay/v1.0.0/llms.txt
> - v0.11.2: https://docs.nvidia.com/nemo/relay/v0.11.2/llms.txt
> - v0.10.4: https://docs.nvidia.com/nemo/relay/v0.10.4/llms.txt
> - v0.9.4: https://docs.nvidia.com/nemo/relay/v0.9.4/llms.txt
> - v0.8.4: https://docs.nvidia.com/nemo/relay/v0.8.4/llms.txt

> For clean Markdown of any page, append .md to the page URL. For a task-oriented documentation index, fetch https://docs.nvidia.com/nemo/relay/llms.txt. Use the documentation version matching the installed NeMo Relay package: latest follows the latest stable release, vX.Y.Z identifies a retained release snapshot, and dev documents development changes. Preserve the selected version when following links; do not mix examples or API contracts from different versions. Fetch the relevant section's /llms.txt before individual pages instead of loading a complete version index.

# Instrument an LLM Call

> Instrument managed LLM calls with NeMo Relay scopes, middleware, events, and subscribers.

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.

#### Prepare

Complete one binding Quick Start guide first:

* [Python Quick Start](/nemo/relay/v1.0.0/getting-started/quick-start/python)
* [Node.js Quick Start](/nemo/relay/v1.0.0/getting-started/quick-start/nodejs)
* [Rust Quick Start](/nemo/relay/v1.0.0/getting-started/quick-start/rust)

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](/nemo/relay/v1.0.0/instrument-applications/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.

#### 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.

#### 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.

#### Python

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

```python title={"llm_call.py"} maxLines={0} highlight={[29, 30, 31, 32, 33, 34, 35]}
# 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`](/nemo/relay/v1.0.0/reference/api/python-library-reference/nemo-relay/llm#execute).

Run it from the project directory:

```bash
uv run python llm_call.py
```

#### Node.js

Save the following code as `llm-call.cjs` in your Quick Start project:

```js title={"llm-call.cjs"} maxLines={0} highlight={[26]}
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

const {
  LlmRequest,
  ScopeType,
  deregisterSubscriber,
  flushSubscribers,
  llmCallExecute,
  registerSubscriber,
  withScope,
} = require('nemo-relay-node');

function provider(request) {
  return { text: 'hello', messages: request.content.messages };
}

async function main() {
  registerSubscriber('llm-check', (event) => {
    console.log(`${event.kind} ${event.name}`);
  });

  try {
    return await withScope('agent-run', ScopeType.Agent, async (handle) => {
      const request = new LlmRequest({}, { messages: [{ role: 'user', content: 'hello' }] });
      const result = await llmCallExecute('demo-provider', request, provider, handle, null, null, null, 'demo-model');

      console.log(result);
      return result;
    });
  } finally {
    await flushSubscribers();
    deregisterSubscriber('llm-check');
  }
}

module.exports = { main, provider };

if (require.main === module) {
  main().catch((error) => {
    console.error(error);
    process.exitCode = 1;
  });
}

```

API reference: [`llmCallExecute`](/nemo/relay/v1.0.0/reference/api/nodejs-library-reference/nemo-relay-node#llmcallexecute).

Run it from the project directory:

```bash
node llm-call.cjs
```

#### Rust

Use the following code as `src/main.rs` in your Quick Start project:

```rust title={"src/main.rs"} maxLines={0} highlight={[31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47]}
// SPDX-FileCopyrightText: Copyright (c) 2026, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use nemo_relay::llm::{LlmCallExecuteParams, LlmRequest, llm_call_execute};
use nemo_relay::scope::{self, PopScopeParams, PushScopeParams, ScopeAttributes, ScopeType};
use nemo_relay::subscriber::{deregister_subscriber, flush_subscribers, register_subscriber};
use serde_json::json;
use std::sync::Arc;

#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
    register_subscriber(
        "llm-check",
        Arc::new(|event| {
            println!("{} {}", event.kind(), event.name());
        }),
    )?;

    let handle = scope::push_scope(
        PushScopeParams::builder()
            .name("agent-run")
            .scope_type(ScopeType::Agent)
            .attributes(ScopeAttributes::empty())
            .build(),
    )?;

    let request = LlmRequest {
        headers: Default::default(),
        content: json!({"messages": [{"role": "user", "content": "hello"}]}),
    };
    let result = llm_call_execute(
        LlmCallExecuteParams::builder()
            .name("demo-provider")
            .request(request)
            .func(Arc::new(|request| {
                Box::pin(async move {
                    Ok(json!({
                        "text": "hello",
                        "messages": request.content["messages"]
                    }))
                })
            }))
            .parent(handle.clone())
            .model_name("demo-model")
            .build(),
    )
    .await;

    scope::pop_scope(PopScopeParams::builder().handle_uuid(&handle.uuid).build())?;
    flush_subscribers()?;
    let _ = deregister_subscriber("llm-check")?;
    println!("{}", result?);
    Ok(())
}

```

API reference: [`llm_call_execute`](/nemo/relay/v1.0.0/reference/api/rust-library-reference/nemo-relay/llm/fn-llm-call-execute).

Run it from the project directory containing `Cargo.toml`:

```bash
cargo run
```

#### Verify

The subscriber prints these event lines for the local callback:

**`Expected event lines`**

```text title="Expected event lines"
scope agent-run
scope demo-provider
scope demo-provider
scope agent-run
```

The returned payload contains:

**`Expected result`**

```json title="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](/nemo/relay/v1.0.0/integrate-into-frameworks/provider-response-codecs#cost-estimation).

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.

* Instrument tools with [Instrument a Tool Call](/nemo/relay/v1.0.0/instrument-applications/instrument-tool-call).
* Add policy or transformation with [Add Middleware](/nemo/relay/v1.0.0/instrument-applications/advanced-guide). To choose which stages run for one call, refer to
  [Configure Execution Stages per Call](/nemo/relay/v1.0.0/instrument-applications/advanced-guide#configure-execution-stages-per-call).
* Use [Provider Codecs](/nemo/relay/v1.0.0/integrate-into-frameworks/provider-codecs) when request
  intercepts need normalized LLM request data or downstream consumers need
  normalized response annotations.
* Export events with [Observability](/nemo/relay/v1.0.0/configure-plugins/observability/about).