# Token Streaming

Feed partial responses in real time as they become available, engaging users immediately and allowing them to consume content sooner. This feature is available from [`CapabilitiesVersion`](https://developer.salesforce.com/docs/service/messaging-api/references/miaw-api-reference?meta=type%3ACapabilitiesVersion) 63. Streaming tokens have 2 sets of payloads: a Messaging streaming tokens payload and a streaming tokens validation payload.

## Sample Payload

```sfdocs-code {"lang":"json", "title": "Messaging Streaming Tokens Sample Payload" }
{
  "channelPlatformKey": "42b3f2ec-9ab1-40ae-b97d-ebb42650826e",
  "channelType": "embedded_messaging",
  "channelAddressIdentifier": "42b3f2ec-9ab1-40ae-b97d-ebb42650826e",
  "conversationId": "0db1b9a6-96bf-46bd-b088-182d05522e90",
  "conversationEntry": {
    "senderDisplayName": "Agentforce",
    "identifier": "5f1085c6-e933-49d3-bf87-f7d52fac149a",
    "entryType": "StreamingToken",
    "entryPayload": "{\"entryType\":\"StreamingToken\",\"id\":\"5f1085c6-e933-49d3-bf87-f7d52fac149a\",\"streamingToken\":{\"tokenType\":\"MessageStreamingToken\",\"sequenceNumber\":1,\"timestamp\":1741299980669,\"targetMessageIdentifier\":\"1741299974320-REQ-1\",\"token\":{\"formatType\":\"Text\",\"text\":\"Agentforce is a powerful tool that helps companies build AI agents to drive customer\"}}}",
    "sender": {
      "role": "Chatbot",
      "appType": "chatbot",
      "subject": "0XxHx0000001srB",
      "clientIdentifier": ""
    },
    "contextParamMap": {},
    "visibilityStrategy": "Default",
    "transcriptedTimestamp": 1741299980880,
    "relatedRecords": [
      "0MwHx00000A1QKT"
    ],
    "clientTimestamp": 1741299980670,
    "clientDuration": 0
  }
}
```

Message streaming tokens have to be grouped and ordered in the client.

1. The `id` field contains a unique identifier (uuid) for each token.
2. All tokens in a streaming set have the same `targetMessageIdentifier` value. This field value is the same as the `id` value of the final message.
3. Each token in a streaming set has a unique, increasing integer value in the `sequenceNumber` field to denote the order of that particular token in the token set. The tokens in the streaming set can come out of order, and you use the `sequenceNumber` value to arrange the tokens in the right order.

## Streaming Token Validation Payload

```sfdocs-code {"lang":"json", "title": "Streaming Token Validation Sample Payload" }
{
  "channelPlatformKey": "fe3cc3e1-14b3-4c6e-b436-f05a8f7142aa",
  "channelType": "embedded_messaging",
  "channelAddressIdentifier": "fe3cc3e1-14b3-4c6e-b436-f05a8f7142aa",
  "conversationId": "38bf8138-5e13-4503-992c-d648b3ec6e01",
  "conversationEntry": {
    "senderDisplayName": "Agentforce Service Agent",
    "identifier": "8b764460-fde7-4ad4-a9f9-b9ed85528542",
    "entryType": "StreamingToken",
    "entryPayload": "{\"entryType\":\"StreamingToken\",\"id\":\"8b764460-fde7-4ad4-a9f9-b9ed85528542\",\"streamingToken\":{\"tokenType\":\"StreamingTokenValidation\",\"sequenceNumber\":1,\"timestamp\":1741302561902,\"result\":{\"valid\":false,\"reason\":\"UNGROUNDED\"},\"targetMessageIdentifier\":\"1741302557255-REQ-1\"}}",
    "sender": {
      "role": "Chatbot",
      "appType": "chatbot",
      "subject": "0XxSG0000008F0H",
      "clientIdentifier": ""
    },
    "contextParamMap": {},
    "visibilityStrategy": "Default",
    "transcriptedTimestamp": 1741302561918,
    "relatedRecords": [
      "0MwSG000009rp0n"
    ],
    "clientTimestamp": 1741302561908,
    "clientDuration": 0
  }
}
```

:::note
Salesforce's trust layer can't validate for hallucinations until the entire response is complete. Although tokens start to stream, the `StreamingTokenValidation` signal notifies you when there's an issue like hallucination with the tokens streamed. You can remove the token streamed upon getting this signal. Another set of tokens will stream after the validation token, you can choose to display those tokens.
:::
