506. Design an Agentic Chatbot With Tool Calls
Asked in
Design an Agentic Chatbot With Tool Calls

Design an agentic chatbot. Users chat with it in conversations. For each query, the chatbot chooses the tools it needs, calls them and builds its reply from their results. A real system would use an LLM to choose the tools. Here, the fixed rules below replace the model, so every reply is deterministic. Keep the tool choice and the tools behind interfaces, so a real LLM or API could replace them without changing the chatbot's flow.

Every tool has a name, keywords, a response template and data that maps an input to an output. The data stands in for the tool's backend. Tools can fail, and a tool can be restricted to certain users.

Methods

AgenticChatbot(String fallbackResponse, int maxRetries, int maxToolCallsPerQuery)
  • fallbackResponse is the reply to a query that selects no tool.
  • After a failed call, a tool is retried at most maxRetries times in the same query.
  • One query makes at most maxToolCallsPerQuery tool calls in total, retries included.
boolean startConversation(String conversationId, String userId)
  • Starts an empty conversation for userId.
  • Returns false if conversationId already exists.
boolean registerTool(String toolName, List<String> keywords, String responseTemplate)
  • Adds a tool with its keywords and response template.
  • responseTemplate may contain {input} and {output}. Both are replaced by the values of a successful call.
  • Returns false if toolName is already registered.
boolean addToolData(String toolName, String input, String output)
  • A successful call to the tool with input returns output. Adding an existing input again replaces its output.
  • Returns false if the tool does not exist.
boolean setToolFailures(String toolName, int failureCount)
  • The next failureCount calls to the tool fail, in any conversation. This replaces any failures still pending for the tool.
  • Returns false if the tool does not exist.
boolean restrictTool(String toolName)
  • From now on, only users granted access can use the tool.
  • Returns false if the tool does not exist.
boolean grantToolAccess(String userId, String toolName)
  • Lets userId use the tool. Access can be granted before or after the tool is restricted.
  • Returns false if the tool does not exist.
String sendQuery(String conversationId, String query)
  • Answers the query using the rules below and returns the reply.
  • Returns "" and records nothing if the conversation does not exist.
List<String> getHistory(String conversationId)
  • Returns the lines recorded in the conversation, oldest first. Returns an empty list if the conversation does not exist.
  • Each line is one of "USER: <query>", "TOOL: <toolName>(<input>) -> <output>", "TOOL: <toolName>(<input>) -> FAILED" or "BOT: <reply>".

How a Query Is Answered

  1. Record the USER line.
  2. Select every tool with a keyword that appears in the query as a whole word. Selected tools are handled in order of the first position where any of their keywords appears. If two tools first appear at the same word, the tool registered earlier goes first. Each tool is handled at most once per query.
  3. Each selected tool adds one sentence to the reply:
    • If the tool is restricted and the conversation's user has no access: "Sorry, you do not have access to <toolName>."
    • Otherwise, find its input: the first word of the query that the tool has data for. If there is none, search the conversation's 5 most recent earlier queries the same way, newest query first. If there is still none: "Please share more details for <toolName>."
    • Otherwise, call the tool if the query's call limit is not used up, and record a TOOL line for every call. After a failed call, retry while the retry limit and the call limit allow. On success, the sentence is the template with every {input} and {output} filled in. If no call succeeds: "Sorry, <toolName> is unavailable right now."
  4. The reply is all sentences joined with single spaces, or fallbackResponse if no tool was selected. Record the BOT line and return the reply.

Constraints

  • conversationId, userId and toolName have 1 to 30 English letters or digits.
  • query has 1 to 20 words separated by single spaces.
  • Every query word, keyword and input has 1 to 20 lowercase English letters or digits.
  • keywords has 1 to 10 items.
  • fallbackResponse and responseTemplate have 1 to 200 characters, and output has 1 to 100 characters. They use printable ASCII characters, and the only braces are {input} and {output} inside templates.
  • 0 ≤ maxRetries ≤ 5
  • 1 ≤ maxToolCallsPerQuery ≤ 100
  • 0 ≤ failureCount ≤ 1,000
  • At most 20 tools, 1,000 conversations and 20,000 method calls in total.

Examples

Example 1

  1. AgenticChatbot(fallbackResponse = "Sorry, I can only help with flights and hotels.", maxRetries = 0, maxToolCallsPerQuery = 5)
  2. registerTool(toolName = "flightStatus", keywords = ["flight", "plane"], responseTemplate = "Flight {input} is {output}.") returns true
  3. addToolData(toolName = "flightStatus", input = "uk811", output = "delayed by 40 minutes") returns true
  4. registerTool(toolName = "hotelPrice", keywords = ["hotel", "stay"], responseTemplate = "One night at {input} costs {output}.") returns true
  5. addToolData(toolName = "hotelPrice", input = "seaview", output = "4500 rupees") returns true
  6. registerTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns true
  7. addToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns true
  8. registerTool(toolName = "hotelPrice", keywords = ["room"], responseTemplate = "Rooms cost {output}.") returns false
  9. addToolData(toolName = "trainStatus", input = "t100", output = "on time") returns false
  10. startConversation(conversationId = "chat1", userId = "riya") returns true
  11. startConversation(conversationId = "chat1", userId = "arjun") returns false
  12. sendQuery(conversationId = "chat1", query = "good morning") returns "Sorry, I can only help with flights and hotels."
  13. sendQuery(conversationId = "chat1", query = "which gate should i go to") returns "Please share more details for gateNumber."
  14. sendQuery(conversationId = "chat1", query = "how much is a stay at seaview and is my plane uk811 late") returns "One night at seaview costs 4500 rupees. Flight uk811 is delayed by 40 minutes."
  15. sendQuery(conversationId = "chat5", query = "is my flight late") returns ""
  16. getHistory(conversationId = "chat1") returns ["USER: good morning", "BOT: Sorry, I can only help with flights and hotels.", "USER: which gate should i go to", "BOT: Please share more details for gateNumber.", "USER: how much is a stay at seaview and is my plane uk811 late", "TOOL: hotelPrice(seaview) -> 4500 rupees", "TOOL: flightStatus(uk811) -> delayed by 40 minutes", "BOT: One night at seaview costs 4500 rupees. Flight uk811 is delayed by 40 minutes."]

Explanation:

  • Call 13: gateNumber is selected, but neither this query nor the earlier one has a word gateNumber has data for.
  • Call 14: "stay" comes before "plane", so hotelPrice is handled before flightStatus, even though flightStatus was registered first.
  • Call 15: conversation chat5 does not exist.

Example 2

  1. AgenticChatbot(fallbackResponse = "I can only help with flights.", maxRetries = 1, maxToolCallsPerQuery = 3)
  2. registerTool(toolName = "flightStatus", keywords = ["flight"], responseTemplate = "Flight {input} is {output}.") returns true
  3. addToolData(toolName = "flightStatus", input = "ai202", output = "on time") returns true
  4. addToolData(toolName = "flightStatus", input = "uk811", output = "delayed by 40 minutes") returns true
  5. registerTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns true
  6. addToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns true
  7. addToolData(toolName = "gateNumber", input = "uk811", output = "C4") returns true
  8. registerTool(toolName = "seatUpgrade", keywords = ["upgrade"], responseTemplate = "Seat upgrade on flight {input} is {output}.") returns true
  9. addToolData(toolName = "seatUpgrade", input = "ai202", output = "confirmed") returns true
  10. restrictTool(toolName = "seatUpgrade") returns true
  11. grantToolAccess(userId = "riya", toolName = "seatUpgrade") returns true
  12. restrictTool(toolName = "mealOrder") returns false
  13. startConversation(conversationId = "chat1", userId = "riya") returns true
  14. startConversation(conversationId = "chat2", userId = "arjun") returns true
  15. sendQuery(conversationId = "chat1", query = "is flight ai202 on time") returns "Flight ai202 is on time."
  16. sendQuery(conversationId = "chat1", query = "and flight uk811") returns "Flight uk811 is delayed by 40 minutes."
  17. sendQuery(conversationId = "chat1", query = "which gate do i use") returns "Flight uk811 boards at gate C4."
  18. sendQuery(conversationId = "chat2", query = "which gate do i use") returns "Please share more details for gateNumber."
  19. sendQuery(conversationId = "chat1", query = "please upgrade my seat on ai202") returns "Seat upgrade on flight ai202 is confirmed."
  20. sendQuery(conversationId = "chat2", query = "upgrade my seat on flight ai202") returns "Sorry, you do not have access to seatUpgrade. Flight ai202 is on time."

Explanation:

  • Call 17: the query names no flight, so the 5 most recent earlier queries of chat1 are searched, newest first. "and flight uk811" is the newest one with a word gateNumber has data for.
  • Call 18: chat2 has no earlier queries, and other conversations are never searched.
  • Call 20: arjun has no access to seatUpgrade, so it is not called. flightStatus still answers.

Example 3

  1. AgenticChatbot(fallbackResponse = "I can help with flights and hotels.", maxRetries = 2, maxToolCallsPerQuery = 4)
  2. registerTool(toolName = "flightStatus", keywords = ["flight"], responseTemplate = "Flight {input} is {output}.") returns true
  3. addToolData(toolName = "flightStatus", input = "ai202", output = "on time") returns true
  4. registerTool(toolName = "hotelPrice", keywords = ["hotel"], responseTemplate = "One night at {input} costs {output}.") returns true
  5. addToolData(toolName = "hotelPrice", input = "seaview", output = "4500 rupees") returns true
  6. registerTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns true
  7. addToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns true
  8. startConversation(conversationId = "chat7", userId = "meera") returns true
  9. setToolFailures(toolName = "flightStatus", failureCount = 2) returns true
  10. setToolFailures(toolName = "visaRules", failureCount = 1) returns false
  11. sendQuery(conversationId = "chat7", query = "status of flight ai202") returns "Flight ai202 is on time."
  12. setToolFailures(toolName = "hotelPrice", failureCount = 5) returns true
  13. sendQuery(conversationId = "chat7", query = "hotel seaview price and gate for flight ai202") returns "Sorry, hotelPrice is unavailable right now. Flight ai202 boards at gate B12. Sorry, flightStatus is unavailable right now."
  14. getHistory(conversationId = "chat7") returns ["USER: status of flight ai202", "TOOL: flightStatus(ai202) -> FAILED", "TOOL: flightStatus(ai202) -> FAILED", "TOOL: flightStatus(ai202) -> on time", "BOT: Flight ai202 is on time.", "USER: hotel seaview price and gate for flight ai202", "TOOL: hotelPrice(seaview) -> FAILED", "TOOL: hotelPrice(seaview) -> FAILED", "TOOL: hotelPrice(seaview) -> FAILED", "TOOL: gateNumber(ai202) -> B12", "BOT: Sorry, hotelPrice is unavailable right now. Flight ai202 boards at gate B12. Sorry, flightStatus is unavailable right now."]
  15. sendQuery(conversationId = "chat7", query = "price of hotel seaview") returns "One night at seaview costs 4500 rupees."

Explanation:

  • Call 11: the first call and the first retry fail, and the second retry succeeds.
  • Call 13: hotelPrice fails 3 times (1 call and 2 retries). gateNumber makes the 4th call, so no calls are left for flightStatus.
  • Call 15: hotelPrice still has 2 pending failures, so the first call and the first retry fail and the second retry succeeds. Each query gets a new call limit.

Example 4

  1. AgenticChatbot(fallbackResponse = "I can tell you your gate.", maxRetries = 0, maxToolCallsPerQuery = 1)
  2. registerTool(toolName = "gateNumber", keywords = ["gate"], responseTemplate = "Flight {input} boards at gate {output}.") returns true
  3. addToolData(toolName = "gateNumber", input = "ai202", output = "B12") returns true
  4. startConversation(conversationId = "chat3", userId = "kabir") returns true
  5. sendQuery(conversationId = "chat3", query = "i am on ai202") returns "I can tell you your gate."
  6. sendQuery(conversationId = "chat3", query = "hello") returns "I can tell you your gate."
  7. sendQuery(conversationId = "chat3", query = "thanks") returns "I can tell you your gate."
  8. sendQuery(conversationId = "chat3", query = "ok") returns "I can tell you your gate."
  9. sendQuery(conversationId = "chat3", query = "great") returns "I can tell you your gate."
  10. sendQuery(conversationId = "chat3", query = "which gate") returns "Flight ai202 boards at gate B12."
  11. sendQuery(conversationId = "chat3", query = "gate again") returns "Please share more details for gateNumber."

Explanation:

  • Call 10: call 5 is the 5th most recent earlier query, so its "ai202" is still found.
  • Call 11: call 5 is now the 6th most recent earlier query, so it is not searched. None of the 5 most recent earlier queries has a word gateNumber has data for.


Please use Laptop/Desktop or any other large screen to add/edit code.