This page explains how scope stacks establish ownership, parentage, cleanup, and isolation.
Scopes are the ownership backbone of NeMo Relay. Every tool call, LLM call, and mark event attaches to a scope hierarchy.
That hierarchy lets the runtime:
A scope represents a logical unit of work such as:
Scopes are not just labels. They define ownership and visibility for other runtime behavior.
Scopes form a tree. A child scope inherits the active execution context from its parent and contributes new nested work beneath it.
That hierarchy determines:
NeMo Relay includes standard scope types for common runtime semantics, including:
AgentFunctionToolLlmRetrieverEmbedderRerankerGuardrailEvaluatorCustomUnknownThe specific type helps subscribers and downstream tracing systems understand what the scope represents semantically.
These scope behaviors define how root, child, and scope-local runtime state interact.
A root scope is always present. Other scopes are pushed beneath that root as work becomes more specific.
Nested scopes create the ownership tree used by emitted events. Tool and LLM calls then attach beneath the active scope.
Scopes have explicit lifetime boundaries. A scope starts when it becomes active and ends when it is popped or closed.
Scope-local middleware and subscribers are tied to the owning scope lifecycle. When the scope closes, those registrations disappear automatically.
Scopes may expose semantic input and output payloads on their emitted start
and end events.
Use scope input when the scope itself represents a request-style or task-style
unit of work whose starting payload matters semantically.
Use scope output when the scope itself produces a meaningful semantic result.
Those payloads live on the emitted events rather than on the scope handle itself.
Context isolation keeps concurrent requests, tenants, and agents from sharing scope- local state accidentally.
Concurrent requests must not share the same active scope stack accidentally. Otherwise:
Reuse or propagate the active scope stack when detached work should continue the same logical request or agent trace.
Use this when:
Create and bind a fresh stack when detached work should be independent.
Use this when:
Use these practices when applying the concept in application or integration code.