How it works
User input follows a channel-based pattern where both users and agents connect to a shared channel:- The agent subscribes to the channel to listen for user messages.
- The user publishes a message containing their prompt.
- The agent receives the message, processes it, and generates a response.
- The agent publishes the response back to the channel, correlating it to the original input.
Identify the user
Agents need to verify that incoming messages are from legitimate users. Use identified clients or user claims to establish a verified identity or role for the user.Verify by user identity
Use theclientId to identify the user who sent a message. This enables personalized responses, per-user rate limiting, or looking up user-specific preferences from your database.
When a user authenticates with Ably, embed their identity in the JWT:
The clientId is automatically attached to every message the user publishes, so agents can trust this identity.
Verify by role
Use user claims to verify that a message comes from a user rather than another agent sharing the channel. This is useful when the agent needs to distinguish message sources without needing the specific user identity. When a user authenticates with Ably, embed their role in the JWT: The user claim is automatically attached to every message the user publishes, so agents can trust this role information.Publish user input
Users publish messages to the channel to send prompts to the agent. Generate a uniquepromptId for each message to correlate agent responses back to the original prompt.
Subscribe to user input
The agent subscribes to a channel to receive messages from users. When a user publishes a message to the channel, the agent receives it through the subscription callback. The following example demonstrates an agent subscribing to receive user input:Publish agent responses
When the agent sends a response, it includes thepromptId from the original input so users know which prompt the response relates to. This is especially important when users send multiple prompts in quick succession or when responses are streamed.
Use the extras.headers field to include the promptId in agent responses:
The user’s client can then match responses to their original prompts:
Stream responses
For longer AI responses, you’ll typically want to stream tokens back to the user rather than waiting for the complete response. ThepromptId correlation allows users to associate streamed tokens with their original prompt.
When streaming tokens using message-per-response or message-per-token patterns, include the promptId in the message extras:
