> This page is for version Dev.
> For other versions, use one of these documentation indexes:
> - Latest (v0.4.0) (default): https://docs.nvidia.com/nemo/switchyard/latest/llms.txt
> - Dev: https://docs.nvidia.com/nemo/switchyard/dev/llms.txt

> For clean Markdown of any page, append .md to the page URL.
> For a complete documentation index, see https://docs.nvidia.com/nemo/switchyard/llms.txt.
> For AI client integration (Claude Code, Cursor, etc.), connect to the MCP server at https://docs.nvidia.com/nemo/switchyard/_mcp/server.

# LLM-Related Protocols

An LLM request describes a conversation, the tools available to the model, and
generation settings. Switchyard represents these in
[`LlmRequest`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.LlmRequest.html).
The same type works whether the next call goes to Anthropic Messages, OpenAI
Responses, or Chat Completions.

> **Info**
>
> **Fields, API mappings, and conversion rules**
>
> * **[Request reference](/nemo/switchyard/dev/reference/protocols/llm/requests):** messages, tools, generation settings, and their provider API fields.
> * **[Response and stream reference](/nemo/switchyard/dev/reference/protocols/llm/responses-and-streams):** output content, token usage, event types, and stream lifetime.
> * **[Translation reference](/nemo/switchyard/dev/reference/protocols/llm/translation):** engine methods, preservation policies, diagnostics, and edits before encoding.

## Read a request \[#read-a-request]

[`LlmRequest`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.LlmRequest.html)
separates instructions from conversation turns. A
[`Message`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.Message.html)
contains a role and ordered
[`ContentBlock`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/enum.ContentBlock.html)
values. Content can include text, media, reasoning, tool calls, and tool results.

This example constructs the neutral request passed to an algorithm. It
uses the [quickstart's dependencies](/nemo/switchyard/dev/get-started/getting-started#install).

```rust
use switchyard_protocol::{ContentBlock, InstructionBlock, LlmRequest, Message, Request, Role};

let request = Request {
    llm_request: LlmRequest {
        instructions: vec![InstructionBlock {
            role: Role::System,
            content: vec![ContentBlock::Text { text: "Answer briefly.".into() }],
        }],
        messages: vec![Message::text(Role::User, "Explain tail latency.")],
        ..LlmRequest::default()
    },
    ..Request::default()
};
```

[`Request`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/envelope/struct.Request.html) is the host-facing envelope. It adds optional correlation metadata
and [`raw_request`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/envelope/struct.Request.html#structfield.raw_request), a host-owned JSON body. **Codecs use
[`LlmRequest::preservation`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.LlmRequest.html#structfield.preservation) for replay**, not [`Request::raw_request`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/envelope/struct.Request.html#structfield.raw_request).

## Read a response \[#read-a-response]

The host returns a [`Response`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/envelope/struct.Response.html) envelope containing
[`LlmResponse`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/stream/enum.LlmResponse.html).
[`LlmResponse::Agg`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/stream/enum.LlmResponse.html#variant.Agg) holds a completed [`AggLlmResponse`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.AggLlmResponse.html). [`LlmResponse::Stream`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/stream/enum.LlmResponse.html#variant.Stream)
holds a single-consumption stream of
[`LlmResponseStreamEvent`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/stream/struct.LlmResponseStreamEvent.html)
values.
Each stream event exposes neutral chunks through [`normalized()`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/stream/struct.LlmResponseStreamEvent.html#method.normalized). Chunks describe
message starts, text deltas, tool arguments, usage, and completion. An event may
also retain parsed provider JSON for same-format replay.

[`Usage::input_tokens`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.Usage.html#structfield.input_tokens) counts **non-cached input**. The codecs separate cache-read
and cache-write tokens from OpenAI's aggregate input counts. Missing counts stay
`None`. Call [`LlmResponse::into_agg().await`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/stream/enum.LlmResponse.html#method.into_agg) when you need a buffered result; this
consumes the stream and aggregates its normalized chunks.

## Understand lossless conversion \[#understand-lossless-conversion]

**The protocol retains source data for lossless same-format round trips.** With
the default preservation policy, decoding a supported API body and encoding it
back to that API reuses its preserved JSON. The
[preservation reference](/nemo/switchyard/dev/reference/protocols/llm/translation#preservation-and-edits)
explains exactly what is retained and when replay takes precedence.

**Conversion between different provider APIs can be lossy.** For example,
Anthropic has no separate developer role, and Chat Completions has no native
equivalent for every Responses built-in tool. Keeping the original data in the
protocol does not give the destination API those capabilities.

Translation reports known losses in [`diagnostics`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationOutput.html#structfield.diagnostics).
The [translation policy reference](/nemo/switchyard/dev/reference/protocols/llm/translation#policies)
explains how to allow or reject these conversions.

> **Warning**
>
> **Encoding an edited request**
>
> A preserved same-format body takes precedence over normalized fields. If you
> change the protocol fields and want those edits encoded, remove that format's
> entry from [`preservation.requests`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.PreservationMetadata.html#structfield.requests), or encode with [`PreservationPolicy::Disabled`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/policy/enum.PreservationPolicy.html#variant.Disabled).
> The same rule applies to [`preservation.responses`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/llm/struct.PreservationMetadata.html#structfield.responses) for buffered responses.

See [stream preservation and aggregation](/nemo/switchyard/dev/reference/protocols/llm/responses-and-streams#stream-lifetime-and-preservation)
for how replay and edits work on streamed responses.

## Use the translation crate \[#use-the-translation-crate]

[`switchyard-translation`](https://github.com/NVIDIA-NeMo/Switchyard/blob/bc716b420f3f76a77c0e93dde08c14b97ec2120f/crates/switchyard-translation/README.md) supplies
the LLM codecs without making network calls. Add it at the same API revision as
libsy and [`switchyard-protocol`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/), following the
[quickstart's translation setup](/nemo/switchyard/dev/get-started/getting-started#use-libsy-with-your-client).

This excerpt decodes an inbound Chat Completions body and encodes it as an
OpenAI Responses request. It can run inside your adapter; [`outbound.body`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationOutput.html#structfield.body) is the
JSON your client sends.

```rust
use serde_json::json;
use switchyard_translation::{TranslationEngine, TranslationPolicy, WireFormat};

let engine = TranslationEngine::default();
let policy = TranslationPolicy::default();
let inbound = json!({
    "model": "your-model",
    "messages": [{"role": "user", "content": "Explain tail latency."}]
});

let decoded = engine.decode_request(WireFormat::OpenAiChat, &inbound, &policy)?;
let outbound = engine.encode_request(
    WireFormat::OpenAiResponses,
    &decoded.request,
    &policy,
)?;
// Check diagnostics from both operations before sending the encoded body.
```

During a libsy run, place [`decoded.request`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.RequestIrOutput.html#structfield.request) in [`Request::llm_request`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/envelope/struct.Request.html#structfield.llm_request). Encode
the request from [`Step::CallModel`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_libsy/enum.Step.html#variant.CallModel) for the selected backend. Decode its reply
with [`decode_response`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationEngine.html#method.decode_response), or use [`decode_stream_event`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationEngine.html#method.decode_stream_event) and [`encode_stream_event`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationEngine.html#method.encode_stream_event)
with a separate [`StreamTranslationState`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/codecs/stream/struct.StreamTranslationState.html) per response stream. Return the neutral
[`Response`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/envelope/struct.Response.html) through the call object, as shown in
[Mechanics of Libsy](/nemo/switchyard/dev/integrate-switchyard/mechanics-of-libsy#stepcallmodel).

The engine also exposes [`translate_request`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationEngine.html#method.translate_request) and [`translate_response`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationEngine.html#method.translate_response) for direct
API-to-API conversion. See the
[translation engine reference](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_translation/engine/struct.TranslationEngine.html)
for signatures and results.

## Keep your own client or conversions \[#keep-your-own-client-or-conversions]

You can construct these protocol types from your existing SDK and convert them
back yourself. When handling [`Step::CallModel`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_libsy/enum.Step.html#variant.CallModel), pass [`call.respond`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_libsy/struct.CallModel.html#method.respond) a future that
uses your client and returns [`libsy::Result<Response>`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_libsy/type.Result.html). Keep content order, tool
IDs, provider-only data, and streaming lifetime intact where your integration
needs them.

[`RoutedLlmClient`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_protocol/client/trait.RoutedLlmClient.html)
is an optional shared interface used by Switchyard's clients and execution
helpers. Direct [`run_stream`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_libsy/trait.Algorithm.html#method.run_stream) handling works with your own functions or clients;
it does not require implementing that trait.

If you want Switchyard to handle HTTP and translation, use
[`TranslatingLlmClient`](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_llm_client/client/struct.TranslatingLlmClient.html)
from [the HTTP client crate](https://nvidia-nemo.github.io/Switchyard/rust/switchyard_llm_client/).
The [quickstart](/nemo/switchyard/dev/get-started/getting-started#use-libsy-with-your-client) shows how to
keep your own HTTP client. Typed decisions use the separate contract described
in [Decision Model-Related Protocols](/nemo/switchyard/dev/integrate-switchyard/switchyard-protocols/decision-model-related-protocols).