Skip to content

Latest commit

 

History

History
355 lines (291 loc) · 16.7 KB

README.md

File metadata and controls

355 lines (291 loc) · 16.7 KB

NVIDIA NeMo Fabric Claude Adapter

The nvidia.fabric.claude adapter uses the official Claude Agent SDK for Python behind NeMo Fabric's normalized invocation contract. The SDK is an implementation detail; consumers select the Claude harness by adapter ID.

The harness extra pins claude-agent-sdk==0.2.166; use the same version for an environment-managed SDK. The SDK supplies a compatible Claude Code runtime.

Install

The following table shows which components each installation provides:

Installation Runtime Adapter Harness NeMo Relay CLI
pip install "nemo-fabric[claude]" Yes Yes Yes Yes
pip install "nemo-fabric-adapters-claude[harness]" No Yes Yes Yes
pip install nemo-fabric-adapters-claude No Yes No No

For split runtime and adapter environments, configure ADAPTER_PYTHON and use matching NeMo Fabric release versions. Refer to the installation guide.

The full extra is equivalent to harness. Both install the supported NeMo Relay CLI so Relay telemetry and Runtime.invoke_stream() work without a separate CLI installation.

Authentication

NeMo Fabric preserves Claude's native credential resolution. Use an existing Claude Code login for local development, ANTHROPIC_AUTH_TOKEN for a gateway or proxy bearer credential, ANTHROPIC_API_KEY for a static API credential, or Anthropic Workload Identity Federation (WIF) for production and CI workloads that should not store a long-lived API key.

The native anthropic provider can use any Claude authentication mode above without an explicit endpoint. For another provider name, configure both models.<role>.api_key_env and models.<role>.base_url. The endpoint must implement the Anthropic Messages protocol; the adapter maps the named credential and endpoint into the environment expected by Claude Code. Provider names identify configuration; the adapter does not maintain a provider allowlist. The runtime-scoped mapping does not change the parent environment.

The adapter forwards the Anthropic profile and federation environment variables that Claude Code and the Claude Agent SDK consume. This includes ANTHROPIC_CONFIG_DIR, ANTHROPIC_PROFILE, the direct federation identifiers, and ANTHROPIC_IDENTITY_TOKEN or ANTHROPIC_IDENTITY_TOKEN_FILE. NeMo Fabric reads selected environment values and forwards them to the Claude runtime, but it does not persist or log them in configuration or artifacts. Authentication is validated when the Claude runtime starts.

Unset unused ANTHROPIC_API_KEY and ANTHROPIC_AUTH_TOKEN variables before using WIF. Anthropic credential resolution treats an empty variable as selected, so an empty API credential prevents fallback to a federation profile.

Refer to the Claude adapter authentication guide for mode selection, required WIF variables, and the Relay boundary. Package installation is verified by the adapter wheel and module-entrypoint tests.

Execution Model

The Claude adapter implements NeMo Fabric's persistent local-host wire protocol. Fabric.start_runtime(...) launches one adapter host, creates one ClaudeSDKClient, and connects it once. Every Runtime.invoke(...) reuses that client and its event loop; Runtime.stop() disconnects the client and exits the host. Fabric.run(...) uses the same lifecycle around one invocation.

One NeMo Fabric runtime maps to one live Claude session. The adapter records the terminal Claude session ID under the NeMo Fabric artifact root for correlation, but does not silently recreate a crashed host or replay an invocation. Start a new NeMo Fabric runtime when the host or SDK connection becomes unusable. Runtime hosting is adapter-declared; consumers do not configure a runtime strategy in FabricConfig or harness.settings.

Configuration

Configure portable capabilities through the normalized FabricConfig fields:

  • models selects the Claude model. The native anthropic provider retains Claude authentication and endpoint discovery. Any other provider name must configure an Anthropic Messages-compatible base_url and api_key_env.
  • instructions.system supports replace and append. replace supplies the complete Claude system prompt. append preserves the claude_code preset and adds the configured content after it.
  • runtime.max_turns sets the Claude turn limit.
  • runtime.timeout_seconds sets the NeMo Fabric invocation deadline. Invocation deadlines use the normalized error code timeout; a Claude SDK request that times out earlier uses claude_timed_out. With a timeout under 10 seconds, NeMo Fabric's own timeout can stop the adapter host while the adapter interrupts Claude, which fails the invocation with a core host_timeout error instead.
  • environment.workspace sets the Claude working directory, and environment.env supplies explicit harness-visible variables.
  • tools.enabled selects Claude built-in tools. None preserves the Claude default, while an empty list disables every tool. With permission_mode set to dontAsk, explicitly enabled tools are also pre-approved so headless runs can invoke them.
  • tools.blocked maps to Claude disallowed_tools. A pre-tool hook enforces both lists across built-in, MCP, and plugin tools.
  • mcp configures stdio, HTTP, streamable HTTP, or SSE servers. For stdio, set url to the executable and pass each command-line argument as a separate args element.
  • Claude Code starts even when some configured MCP servers cannot connect. After each turn the adapter reads Claude Code's own MCP connection state (get_mcp_status()) and reports the configured servers it could not use under output.mcp.unavailable_servers (name, transport, and reason of failed or authentication_required for a server that needs authentication). The adapter writes a warning to standard error the first time a server is unavailable in a runtime. Error messages are omitted because they can carry credentials. Servers that are still connecting are not reported. Each result reflects the current state, so a server that reconnects within the runtime leaves the report, and a new runtime connects every server again.
  • skills.paths names skill directories that contain SKILL.md. The adapter stages these directories as a local Claude plugin for the runtime.

Only Claude-specific controls belong in harness.settings:

Setting Type Required Static default
permission_mode One of default, acceptEdits, bypassPermissions, plan, dontAsk, or auto No No default
reasoning_effort One of low, medium, high, xhigh, or max No Harness default
max_budget_usd Number greater than 0 No No default
max_buffer_size Integer greater than or equal to 1 No Claude Agent SDK default
setting_sources Array containing user, project, or local No []

max_buffer_size sets the size limit the Claude Agent SDK applies to one JSON message from Claude Code, such as a large tool result. A message over the limit fails the invocation instead of being truncated.

Planning validates these settings against the schema in the resolved Claude descriptor. Unknown keys and invalid values fail before the adapter starts. Schema defaults are documentation only; planning preserves the supplied settings without adding setting_sources.

The adapter filters the inherited environment before launching Claude Code. It retains portable OS/config variables, NeMo Relay logging variables (NEMO_RELAY_LOG, NEMO_RELAY_LOG_CONFIG_PATH, NEMO_RELAY_LOG_STDERR, and NEMO_RELAY_LOG_STDERR_FORMAT), the selected model's api_key_env, explicitly configured environment.env values, and variables named by environment.env_names. On SDK startup failure, the existing lifecycle error metadata includes bounded startup_diagnostics summaries for recognized native permission, model, and reasoning-effort rejections. The adapter scans at most 8,192 characters per stderr callback and retains only fixed summaries, without model IDs, credential values, prompts, or environment maps. If stderr has no recognized reason, the metadata contains an omission notice; no stderr leaves the error metadata unchanged. Capture ends after startup. Raw Claude stderr is not persisted as a NeMo Fabric artifact, and runtime journal handling is unchanged.

Token Usage

The result's usage carries Claude Code's input_tokens, output_tokens, and reported cost_usd. Claude Code counts tokens for its main agent loop only: its usage leaves out Task subagent, sidechain, and auxiliary model calls.

Claude Code's input_tokens excludes cache reads and cache writes, so the adapter also returns the nemo_fabric.detailed_token_usage.v1 usage extension, declared under extension_schemas.usage in the descriptor. When Claude Code reports input, cache reads, and cache writes, the extension carries all three as input_tokens, cached_input_tokens, and cache_write_input_tokens with input_tokens_include_cache: false. If any of the three is missing, the extension carries no input counters. Output is reported as output_tokens with output_tokens_include_reasoning: true, because Anthropic bills thinking as output tokens.

Harbor accounting reads these counters before any ATIF trajectory. With the input counters present, Harbor sets n_input_tokens to input plus cache reads plus cache writes and n_cache_tokens to cache reads, from the same main-loop scope as n_output_tokens. Counts the result leaves unknown stay unknown in Harbor; a Relay trajectory's totals are recorded separately and never fill them.

Relay Observability

Relay requires a NeMo Relay CLI in the >=0.9,<0.12 range on PATH. The Claude adapter does not provide a separate relay extra; its harness and full extras install the compatible CLI through nemo-relay-cli-bin. The root nemo-fabric[relay] extra installs only the Relay Python package.

Claude PreToolUse hooks set onFailure: "block" to block tool calls when the Relay hook fails to start, crashes, or times out. This requires Claude Code 2.1.295 or later. The pinned claude-agent-sdk==0.2.166 bundles Claude Code 2.1.299.

Enable Relay through the normalized NeMo Fabric configuration:

config.enable_relay(
    project="fabric-review",
    output_dir="./artifacts/relay",
)

For each Relay-enabled Claude runtime, NeMo Fabric starts one nemo-relay gateway, waits up to 30 seconds for its health endpoint, and stops it with the runtime. NeMo Fabric passes the gateway URL to the connected Claude Code process through ANTHROPIC_BASE_URL and NEMO_RELAY_GATEWAY_URL, and passes the selected explicit model endpoint to the gateway as its Anthropic upstream. It also stages a runtime-scoped Claude plugin that forwards lifecycle hooks with nemo-relay hook-forward claude. Fabric.run(...) starts the same runtime, invokes it once, and stops it, so the gateway has the same lifecycle as that single invocation.

The NeMo Fabric result includes relay_runtime.gateway_config_path, relay_runtime.gateway_log_path, and the collected relay_artifacts. Relay startup failures return a stable adapter error and retain the gateway log for diagnosis.

Credential-free HTTP model gateways

For Relay-enabled runs, NeMo Relay CLI 0.11.x supports explicit trust for HTTP model hosts. Set harness.settings.insecure_http_model_hosts to the exact hostnames or IP addresses of your private inference gateways:

config.harness.settings["insecure_http_model_hosts"] = ["inference.internal", "10.0.0.10"]

Entries contain no scheme, port, path, or wildcard. Matching ignores DNS case and a trailing dot, permits any port or path on that host, and does not infer trust from DNS resolution. The adapter passes this list to Relay's runtime [upstream] configuration. An explicit empty list clears inherited HTTP trust; omitting the setting preserves Relay's existing configuration behavior. System Relay configuration takes precedence and can enforce an empty list.

This path is for unauthenticated model endpoints: Relay strips authorization, API-key, cookie, and other credential headers before remote HTTP forwarding, including model credentials supplied to the harness. Relay rejects conflicting upstream authorization configuration. Prompts and responses still travel over plaintext HTTP. HTTPS remains the default. This setting affects Relay-enabled Codex, Claude, and Pi model forwarding; MCP policy is unchanged. Older Relay CLIs reject the setting with an upgrade message.

Typed Configuration

Build the agent configuration with the typed SDK models before invoking NeMo Fabric:

from pathlib import Path

from nemo_fabric import (
    EnvironmentConfig,
    Fabric,
    FabricConfig,
    HarnessConfig,
    InstructionConfig,
    InstructionsConfig,
    McpConfig,
    McpServerConfig,
    MetadataConfig,
    ModelConfig,
    RuntimeConfig,
    SkillConfig,
    ToolsConfig,
)

base_dir = Path("/workspace/review-agent")
config = FabricConfig(
    metadata=MetadataConfig(name="claude-review-agent"),
    harness=HarnessConfig(
        adapter_id="nvidia.fabric.claude",
        resolution="preinstalled",
        settings={
            "permission_mode": "dontAsk",
        },
    ),
    models={
        "default": ModelConfig(
            provider="anthropic",
            model="your-claude-model",
            api_key_env="ANTHROPIC_API_KEY",
        )
    },
    instructions=InstructionsConfig(
        system=InstructionConfig(
            content="Review changes for correctness and regressions.",
            mode="replace",
        )
    ),
    runtime=RuntimeConfig(
        artifacts="./artifacts",
        timeout_seconds=600,
        max_turns=8,
    ),
    environment=EnvironmentConfig(provider="local", workspace="."),
    tools=ToolsConfig(
        enabled=["Read", "Edit", "Bash"],
        blocked=["WebFetch"],
    ),
    mcp=McpConfig(
        servers={
            "repo": McpServerConfig(
                transport="stdio",
                url="repo-mcp",
                args=["--root", "."],
                exposure="harness_native",
            )
        }
    ),
    skills=SkillConfig(paths=["./skills/code-review"]),
)

fabric = Fabric()

Single Invocation

result = await fabric.run(
    config,
    base_dir=base_dir,
    input="Inspect the repository",
)

print(result.output["response"])
print(result.output["session_id"])

Multi-Turn Runtime

async with await fabric.start_runtime(config, base_dir=base_dir) as runtime:
    first = await runtime.invoke(input="Inspect the repository")
    second = await runtime.invoke(input="Now review the latest patch")

assert first.runtime_id == second.runtime_id
assert first.output["session_id"] == second.output["session_id"]

The runtime must remain on the same local host for its lifetime. A persisted NeMo Fabric-to-Claude correlation record is not an attach token and cannot recover a stopped or crashed local host.

Health Checks

Runtime checks support three cumulative levels, including during an invocation:

Level Native evidence
live The owned Claude Code subprocess has not exited.
active The connected CLI returns a current, valid get_mcp_status() response.
ready A current, valid status response from the initialized CLI serves as a qualified equivalent of a native health response.

Active and ready use the same native status probe for this MVP. The health result describes initialized CLI responsiveness; MCP connection states remain native configuration diagnostics.

Refer to the SDK health-check overview for shared behavior and a comparison of native evidence across adapters.

Testing

The default suite uses deterministic mock Claude Code and NeMo Relay CLIs and requires no credentials. Test a current nemo-relay CLI with the mock Claude client, or run the live integrations on an authenticated developer host:

FABRIC_NEMO_RELAY_COMMAND="$(command -v nemo-relay)" uv run --no-sync pytest tests/e2e/test_claude.py -q -k real_relay_gateway
RUN_FABRIC_CLAUDE_INTEGRATION=1 uv run --no-sync pytest tests/e2e/test_claude.py -q -k live
RUN_FABRIC_CLAUDE_RELAY_INTEGRATION=1 uv run --no-sync pytest tests/e2e/test_claude.py -q -k live_claude_relay

Set FABRIC_TEST_CLAUDE_MODEL to override the default live-test model, claude-sonnet-4-5.

The live NeMo Relay test applies the same semantic artifact contract as Codex: ATOF must contain structured LLM requests and token usage, and ATIF must contain the expected agent response.