Written By William Bowen
Last updated About 1 month ago
Intro
How do we build an agent? This article shows the step by step process of building a flow in Clerk Chat. We cover:
What is the goal of the agent?
Logic breakdown + planning
Building in Clerk Chat
It is recommended to approach building agents in this order!
Goal
Create an Agent that converses with a user and answers their FAQs on Clerk Chat
Logic
Before building in agents we need to map out the logic. How do we want the AI to think about this problem?
The ‘on-topic’ conversations will be asking a question about Clerk Chat. In this case, we want the AI to send back an answer.
If the message is not a question about Clerk Chat, then we just want to AI to converse back and forth with the user.
Here’s a diagram of that in Figma (great for mapping out AI logic).

This is a great starting point.
We can break it down a bit further by asking, “how would the AI answer their question?”
The AI would have to:
Extract the user’s question (it may be in a sentence and we just want to search for the answer to their specific question, not the whole sentence).
Search the knowledge base for the answer.
Generate an answer.
Send it back to the user.
What about the ‘Converse with the user’ side of the agent?
This just has to read the conversation, generate an appropriate conversational message and send it back to the user.
Our updated logic diagram looks like:

Finally, lets add a bit more info to this diagram.
I want to map out some of the properties of each bot before we build in Agents.
Type
Name
Triggered by
Response type
Prompt
Extract variables (if any).
ℹ️ Note: If you want an explainer on these properties give What are Agents? a read.
This is the logic diagram we end up with:

Explanation:
We only want the top (first) node to be triggered by a user message. We want all other nodes to be triggered only by nodes above them.
We only want the last nodes in each branch to send an SMS message back to the user.
Nodes that don’t send a user message are set to JSON response as this allows for the most manageable structure.
The Knowledge Base Search node is a tool call. A tool is a connection to something other than the AI. In this case, a database of documents. When calling a tool, there is always a tool response.
Now we can build in Agents!
Building
We basically just have to copy the logic and properties we mapped out already. And add a structured prompt using the prompt template here:
Example# Role:
---
# Goal:
---
# Context:
---
# Conversation History
{{#conversationHistory}}
{{#isUser}}Client{{/isUser}}{{^isUser}}Clerk_Chat{{/isUser}}: {{content}}
{{/conversationHistory}}
---
# Company Overview
---
# Instructions:
---
# Example input and output:
📹 Watch this video to see the agent built in our Agent editor from scratch.
The video covers:
Building Nodes.
Connecting edges.
Adding edge logic.
Conclusion
We’ve now built an agent end to end. Testing is the final part of building the agent.