Customer Support Agent
A support agent answers the questions your support team answers every day, at any hour, and hands the rest over to your team.
What you'll build
- A support agent that answers from your help center and stays on topic
- Order lookups and tickets on your backend
- Refunds the user allows first, from a question in the reply
- A ticket for your team when the agent can't help
- A help screen that carries on the user's latest support conversation, with ratings and a button that calls support
Starts from: the Quickstart, with agents, AgentMessage and AgentTransport in your project. It targets iOS 16, for the NavigationStack of its support screen.
Best practices
- Give the agent your help center as knowledge, so it answers from what you published rather than from what the model guesses.
- Keep what reaches your systems, such as orders and refunds, on your backend. A tool on the phone only calls your API, which checks who is signed in.
- Ask before anything that can't be undone, such as a refund.
- Always offer a way to reach a person.
Define the support agent
Make the agent from the text template:
getstream agents init support --template textIts instructions say what it's for:
You are Acme's support assistant. Answer questions about Acme orders, shipping,
returns and billing, briefly.
- Look up an order before you answer a question about it.
- When you can't help, or the customer asks for a person, open a ticket.Its knowledge is your help center, and its guardrail turns away what isn't about Acme:
- https://help.acme.com/shipping
- https://help.acme.com/returns---
refusal: I can only help with questions about your Acme account and orders.
---
Answer questions about Acme orders, billing, shipping and returns. Refuse anything else.Sync it, and try it in the terminal:
getstream agents test support --syncKnowledge & Skills and Guardrails cover both in full.
Host its tools on your backend
The tools that reach your systems run on your backend, in a dispatch worker. Each call knows the user it's for, so a user only finds their own orders:
import { Client } from "@stream-io/stream-agents";
import { z } from "zod";
const client = new Client();
client.tools.register({
name: "lookup_order",
description: "Look up one of the customer's orders",
parameters: z.object({ order_id: z.string() }),
run: ({ order_id }, { user }) => orders.find(order_id, user.id),
});
client.tools.register({
name: "open_ticket",
description: "Hand the conversation over to the support team",
parameters: z.object({ summary: z.string() }),
run: ({ summary }, { user }) => helpdesk.open(user.id, summary),
});
await client.runDispatch();orders and helpdesk stand for your own systems. The worker needs @stream-io/stream-agents 0.8.0 or later, where tools are registered once on client.tools for every agent of the app. Tool calling & Dispatch shows the same worker in other languages.
The agent is offered them once its agent.yaml names them in a tool group. Add one, and sync the agent again:
tools:
- name: support
tools: [lookup_order, open_ticket]
loading: fullAsk before a refund
A tool the worker hosts asks its question only on a voice call. A refund should ask in writing too, so it's a tool the app gives the session. The app asks the user, and once they allow it, the tool calls your backend, which checks the user and the order and issues the refund:
import StreamAgents
let issueRefund = AgentTool(
name: "issue_refund",
description: "Refund one of the customer's orders",
parameters: .strings(
["order_id": "the order's id", "summary": "the order and the amount, such as Order 1042, $39.90"],
required: ["order_id", "summary"]
),
displayTitle: "Refunding your order",
approval: .init(
title: "Refund this order?",
message: "The money goes back to the card the order was paid with.",
reasonArgument: "summary",
allowTitle: "Refund",
declineTitle: "Not now"
)
) { arguments in
let orderID = arguments["order_id"]?.stringValue ?? ""
try await yourBackend.refund(orderID: orderID)
return "Refunded order \(orderID)."
}yourBackend stands for your backend's API. The question shows the model's summary under its title, and a tool that throws tells the model what went wrong, so the agent can say so.
Open the user's support conversation
Carry on the user's latest support session, so the agent has the history, or start one. Pass the refund tool either way, since the session runs it only while the app holds it:
import StreamAgents
@MainActor
func openSupport() async throws -> AgentTransport {
let latest = try await agents.agent("support").sessions.query(limit: 1).items.first
// An incognito session keeps no written conversation.
if let latest, !latest.conversationID.isEmpty {
return try await AgentTransport(agents.attach(sessionID: latest.id, tools: [issueRefund]))
}
return try await AgentTransport(agents.agent("support").sessions.create(SessionOptions(tools: [issueRefund])))
}Build the support screen
The support screen is the quickstart's conversation with common questions while it's empty, and a button that calls support. Its actions keep rating, and leave out editing and regenerating. The button sits in the navigation bar, so the screen brings its own NavigationStack:
import StreamChatAI
import SwiftUI
struct SupportView: View {
@StateObject private var conversation: AIConversation<AgentMessage>
@State private var isCalling = false
init(transport: AgentTransport) {
_conversation = StateObject(
wrappedValue: AIConversation(transport: transport, approver: transport.approver, actions: [.rate])
)
}
var body: some View {
NavigationStack {
AIConversationView(conversation: conversation, emptyContent: {
SuggestionsView(suggestions: ["Where is my order?", "I want to return an item", "Talk to a person"]) {
conversation.send($0)
}
})
.safeAreaInset(edge: .bottom) {
AIComposerView(conversation: conversation)
}
.toolbar {
Button("Call support", systemImage: "phone.fill") { isCalling = true }
}
}
.fullScreenCover(isPresented: $isCalling) {
VoiceAgentView(agent: "support", tools: [issueRefund])
}
}
}Open it the way the quickstart's AssistantScreen does, with openSupport() in place of agents.agent("my-agent").sessions.create(). VoiceAgentView is the call screen of the Voice agent use case, and talks to the same agent, with the same knowledge and tools. The call is a session of its own, so it gets the refund tool too.
The rest comes from the agent and the refund tool, with nothing more to add in the app:
- Order lookups show in the reply as the agent makes them, so the user sees what it checked.
- A refund shows "Refund this order?" under its call, with the order and amount and buttons to allow or decline it. The conversation's approver, from the quickstart's
AgentTransport, sends the answer to the session. Declined, the refund never runs, and the agent is told so. Tool approvals covers the card. - A handover opens a ticket with the agent's summary, so nobody asks the user to start again. The Slack connector lets your team follow along.
Send ratings to your backend
Ratings stay on the device unless the transport sends them. Add a rate(_:_:) to AgentTransport:
import StreamAgents
import StreamChatAI
extension AgentTransport {
func rate(_ reply: AgentMessage, _ rating: AIMessageRating?) async throws {
try await yourBackend.rate(sessionID: session.id, messageID: reply.id, rating: rating)
}
}The router also reviews and scores each session once it ends, which Sessions describes.
Next steps
- Voice agent: the call screen the Call support button opens.
- Purchase a phone number and Transfer to a person: the same agent on the phone.
- Tool calling & Dispatch: tools, approvals, timeouts and progress updates.