Contents

LanguageModelExecutorGenerationChannel

A type you use to send model output deltas and updates to the framework.

Declaration

struct LanguageModelExecutorGenerationChannel

Overview

Use this to stream text as your model produces it. You can also use the channel to report metadata and usage that helps developers track what’s happening, like when you want to report model details and token usage updates:

func respond(
    to request: LanguageModelExecutorGenerationRequest,
    model: MyLanguageModel,
    streamingInto channel: LanguageModelExecutorGenerationChannel
) async throws {

    let entryID = UUID().uuidString

    // Calculate your total and cached tokens counts for the input.
    let totalTokens = 0
    let cachedTokens = 0

    // Send model identification.
    await channel.send(.response(entryID: entryID, action: .updateMetadata([
        "modelID": "my-model-2026-06-08",
        "requestID": request.id.uuidString
    ])))

    // Report prompt token usage upfront.
    await channel.send(.response(
        entryID: entryID,
        action: .updateUsage(
            input: .init(
                totalTokenCount: totalTokens,
                cachedTokenCount: cachedTokens
            ),
            output: .init(
                totalTokenCount: 0,
                reasoningTokenCount: 0
            )
        )
    ))
}

Topics

Creating a channel instance

Sending an event

Accessing the event types

See Also

Custom language model provider