GUIDES
Customer Support Bot
This guide walks you through building a full-featured customer support bot that handles inbound requests, looks up orders, processes returns, and escalates complex issues to human agents.
Architecture Overview
The support bot uses a single agent with four tools: order lookup, return initiation, ticket creation, and human escalation. Conversations persist across sessions so customers can follow up on existing issues.
Step 1: Create the Agent
support-agent.ts
import { YourAutomation } from "@yourautomation/sdk";
const client = new YourAutomation({ apiKey: process.env.YA_API_KEY! });
const supportAgent = await client.agents.create({
name: "Acme Customer Support",
model: "claude-sonnet-4-20250514",
systemPrompt: `You are the primary customer support agent for Acme Corp,
an online electronics retailer.
## Capabilities
- Look up order status and details
- Initiate returns for eligible items
- Create support tickets for complex issues
- Escalate to human agents when needed
## Guidelines
- Always verify the customer's identity (email or order ID) before sharing details
- Be concise, friendly, and professional
- Confirm every action before executing it
- If a return is outside the 30-day window, offer store credit as an alternative
- Escalate immediately for: legal issues, fraud, refunds over $500, complaints
## Response Format
- Keep responses under 200 words
- Use bullet points for multiple items
- Include order numbers and tracking links when available`,
parameters: {
temperature: 0.3,
maxTokens: 1024,
},
memory: {
enabled: true,
maxTurns: 30,
summarize: true,
},
});
Step 2: Define Tools
Tools configuration
const tools = [
{
name: "lookup_order",
description: "Look up order details by order ID or customer email",
parameters: {
type: "object",
properties: {
orderId: { type: "string", description: "Order ID like ORD-12345" },
email: { type: "string", format: "email" },
},
},
handler: "https://api.acme.com/webhooks/orders/lookup",
},
{
name: "initiate_return",
description: "Start a return process for an order item",
parameters: {
type: "object",
properties: {
orderId: { type: "string" },
itemId: { type: "string" },
reason: {
type: "string",
enum: ["defective", "wrong_item", "not_as_described", "changed_mind"],
},
},
required: ["orderId", "itemId", "reason"],
},
handler: "https://api.acme.com/webhooks/returns/initiate",
},
{
name: "create_ticket",
description: "Create a support ticket for issues needing follow-up",
parameters: {
type: "object",
properties: {
subject: { type: "string" },
priority: { type: "string", enum: ["low", "medium", "high"] },
description: { type: "string" },
customerId: { type: "string" },
},
required: ["subject", "priority", "description"],
},
handler: "https://api.acme.com/webhooks/tickets/create",
},
{
name: "escalate",
description: "Escalate to a human agent for complex issues",
parameters: {
type: "object",
properties: {
reason: { type: "string" },
priority: { type: "string", enum: ["normal", "urgent"] },
context: { type: "string", description: "Summary for the human agent" },
},
required: ["reason"],
},
handler: "https://api.acme.com/webhooks/escalate",
},
];
Step 3: Build the Webhook Handlers
Express webhook handlers
import express from "express";
const app = express();
app.use(express.json());
app.post("/webhooks/orders/lookup", async (req, res) => {
const { orderId, email } = req.body.parameters;
// Query your database
const order = await db.orders.findOne({
where: orderId ? { id: orderId } : { customerEmail: email },
});
if (!order) {
return res.json({ error: { code: "NOT_FOUND", message: "Order not found" } });
}
res.json({
result: {
orderId: order.id,
status: order.status,
items: order.items,
total: order.total,
trackingNumber: order.trackingNumber,
deliveredAt: order.deliveredAt,
},
});
});
app.post("/webhooks/returns/initiate", async (req, res) => {
const { orderId, itemId, reason } = req.body.parameters;
const returnRequest = await db.returns.create({
orderId, itemId, reason, status: "pending",
});
res.json({
result: {
returnId: returnRequest.id,
status: "pending",
returnLabel: returnRequest.labelUrl,
instructions: "Print the label and ship within 14 days.",
},
});
});
app.listen(3000);
Step 4: Integrate with Your Frontend
Next.js API route
// app/api/support/route.ts
import { YourAutomation } from "@yourautomation/sdk";
const client = new YourAutomation({ apiKey: process.env.YA_API_KEY! });
const AGENT_ID = "agt_your_support_agent_id";
export async function POST(req: Request) {
const { input, conversationId } = await req.json();
const stream = await client.agents.execute(AGENT_ID, {
input,
conversationId,
stream: true,
});
const encoder = new TextEncoder();
const readable = new ReadableStream({
async start(controller) {
for await (const event of stream) {
controller.enqueue(
encoder.encode(`data: ${JSON.stringify(event)}\n\n`)
);
}
controller.close();
},
});
return new Response(readable, {
headers: { "Content-Type": "text/event-stream" },
});
}
Step 5: Monitor and Optimize
Set up webhooks to monitor your bot's performance:
Monitoring
// Track metrics
await client.webhooks.create({
url: "https://api.acme.com/webhooks/monitoring",
events: [
"execution.completed", // track success rate
"execution.failed", // track errors
"tool.error", // track tool failures
],
agentId: AGENT_ID,
});
Note
Review conversation logs weekly. Look for patterns in escalated conversations to identify gaps in your agent's knowledge that could be addressed with better system prompts or additional tools.
Performance Benchmarks
MetricTarget
First response time< 2 seconds
Resolution rate> 70% without escalation
Customer satisfaction> 4.2 / 5.0
Avg tokens per conversation< 2,000
Escalation rate< 30%