https://retellai.com/?utm_source=discord&utm_medium=seo logo
we have an appointment booking ai agent that calls...
# ❓・ai-evy-support
a
@AI Evy we have an appointment booking ai agent that calls Meta leads. in the meta lead form the leads enter their ZIP codes. The AI is then supossed to select the location closest to their ZIP code among 8 of our locations. We have most of our locations in california, but we have one in Roseville and one in Tigard as well, and we've noticed recently that a lot of the Oregon people (who should be booked in Tigard) are booked in Fullerton, probably since fullerton is geographically the highest of our 5 other locations in california, but it is not closer than Roseville or Tigard, so why is it booking wrong?
a
Thanks for tagging me @Alexander. I'll look through relevant sources and get back to you shortly.
### Answer Based on the available sources, this issue likely stems from how your AI agent is determining the closest location. The LLM may be making incorrect geographic assumptions rather than using precise distance calculations. [(1) ]() Recommended Solutions: 1. Use a Code Tool with precise ZIP code distance calculation [(2) ]() Instead of relying on the LLM to determine proximity, implement a Code Tool that calculates actual distances between the lead's ZIP code and your 8 locations. The Code Tool can use the
fetch()
function to call a geocoding API that returns accurate distances. 2. Pre-calculate and inject location data as dynamic variables Before the call starts, use your external system to: - Calculate distances from the lead's ZIP code to all 8 locations - Determine the closest location - Inject this as a dynamic variable (e.g.,
{{closest_location}}
) at call creation [(1) ]() [(3) ]() This approach ensures the agent has the correct location before the conversation begins, eliminating LLM guesswork. [(3) ]() 3. Lock down the prompt to prevent hallucination Add explicit instructions to your agent prompt:
Copy code
CRITICAL RULE:
You must ONLY book appointments at the location specified in {{closest_location}}.
You are not allowed to guess, infer, or suggest other locations.
If closest_location is missing, politely end the call.
4. Use function calling with strict location validation If using function calling for booking, ensure your function validates that the selected location matches the pre-calculated closest location. [(4) ]() Return an error if the agent attempts to book at the wrong location, forcing it to use the correct one. Why this is happening: LLMs can struggle with geographic reasoning, especially when locations span multiple states. [(5) ]() The agent may be using simple heuristics (like "northernmost California location") rather than actual distance calculations, causing Oregon leads to be incorrectly routed to Fullerton instead of Tigard.
​ To learn more, see the sources I considered: [[1] 7 Personalization Tactics That Boost Call-to-Booking Rates 41% in AI-Powered Appointment Scheduling](https://www.retellai.com/resources/7-tactics-boost-call-to-booking-rates) [[2] Code Tool](https://docs.retellai.com/build/single-multi-prompt/code-tool) [[3] GHL + Make + Retell](https://community.retellai.com/t/ghl-make-retell/806) [[4] Integrate Function Calling](https://docs.retellai.com/integrate-llm/integrate-function-calling) [[5] Training & Customizing AI Voice Agents | Retell AI](https://www.retellai.com/blog/training-and-customizing-voice-agents-with-retell-ai) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
a
@AI Evy How about I just create a simple MD file that has literally every ZIP code on the west and next to it the closest clinic name, that was its hard code?
a
Thanks for the follow-up, @Alexander. I'll get back to you soon.
### Answer Creating a hardcoded ZIP code mapping is a solid approach, but you'll need to implement it correctly in Retell AI. Here are your best options: Option 1: Code Tool with ZIP lookup [(1) ]() Create a Code Tool that looks up the ZIP code in your mapping. You can embed the mapping directly in the JavaScript code:
Copy code
javascript
// ZIP to clinic mapping
const zipMapping = {
  "97224": "Tigard",
  "97223": "Tigard",
  "92832": "Fullerton",
  // ... add all your ZIP codes
};

const zipCode = dv.zip_code;
const clinic = zipMapping[zipCode] || "Unknown";

return { closest_clinic: clinic };
Then use Store Fields as Variables to save the result as
{{closest_clinic}}
for use in your agent's prompt. Option 2: Custom Function with external lookup [(2) ]() If your mapping file is large, host it externally and use a Custom Function to query it:
Copy code
javascript
const response = await fetch("<https://yourdomain.com/zip-clinic-mapping.json%22)> ;
const mapping = await response.json() ;
const clinic = mapping[dv.zip_code];
return { closest_clinic: clinic };
Option 3: Pre-calculate before the call [(3) ]() The most reliable approach: look up the ZIP code in your mapping before creating the call, then pass the result as a dynamic variable when you create the call via the API. This eliminates any LLM guesswork entirely. Important considerations: - Access dynamic variables using the
dv
object (e.g.,
dv.zip_code
) . [(1) ]() - Make sure you extract the ZIP code from the lead form and pass it as a dynamic variable at call creation. [(3) ]() - In your prompt, explicitly instruct the agent to use
{{closest_clinic}}
for booking—never let it choose independently. The hardcoded mapping approach will work well and eliminate the geographic reasoning issues you're experiencing.
​ To learn more, see the sources I considered: [[1] Code Tool](https://docs.retellai.com/build/single-multi-prompt/code-tool) [[2] Integrate any system with custom function](https://docs.retellai.com/build/single-multi-prompt/custom-function) [[3] Dynamic Variables](https://docs.retellai.com/build/dynamic-variables) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
a
@AI Evy do you recommend that approach or your first suggestion?
a
Thanks for the follow-up, @Alexander. I'll get back to you soon.
### Answer I recommend the hardcoded ZIP mapping approach (your suggestion) , specifically implemented as Option 3: Pre-calculate before the call. [(1) ]() Why this is the best approach: 1. Most reliable - Eliminates LLM decision-making entirely. The correct clinic is determined before the call even starts, so there's zero chance of the agent making geographic reasoning errors. 2. Fastest - No function calls or lookups during the conversation. The
{{closest_clinic}}
variable is already available when the call begins. 3. Simplest to maintain - Your ZIP mapping logic lives in one place (your backend/workflow) , not embedded in Retell's Code Tool JavaScript. [(2) ]() Implementation: When you receive a Meta lead with a ZIP code, look it up in your mapping file, then pass the result when creating the call: [(1) ]()
Copy code
json
{
  "retell_llm_dynamic_variables": {
    "zip_code": "97224",
    "closest_clinic": "Tigard"
  }
}
Then in your agent prompt, add:
Copy code
CRITICAL RULE:
You must book this appointment at {{closest_clinic}}.
Do not suggest or consider any other location.
This approach gives you full control over the clinic selection logic and prevents the LLM from overriding your business rules.
​ To learn more, see the sources I considered: [[1] Dynamic Variables](https://docs.retellai.com/build/dynamic-variables) [[2] Code Tool](https://docs.retellai.com/build/single-multi-prompt/code-tool) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
a
@AI Evy how long approx will it take to set this up?
a
Thanks for the follow-up, @Alexander. I'll get back to you soon.
### Answer
message has been deleted
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.
a
@AI Evy how long approx will it take to set this up?
a
Thanks for the follow-up, @Alexander. I'll get back to you soon.
### Answer I don't have information about implementation timelines in the available sources. However, I can break down the technical steps based on the documentation: Setup Steps: 1. Create your ZIP-to-clinic mapping - This is your custom data file (time depends on how many ZIP codes you need to map) 2. Modify your call creation logic - When you receive a Meta lead, look up the ZIP code in your mapping and pass it as a dynamic variable [(1) ]() 3. Update your agent prompt - Add the critical rule to use only
{{closest_clinic}}
The Retell-specific configuration is straightforward - you're just passing dynamic variables when creating calls and updating your prompt. The main time investment will be building and maintaining your ZIP code mapping file. If you need a faster initial test, you could start with just the ZIP codes for your problem areas (Oregon and the California locations being confused) to validate the approach works before mapping every West Coast ZIP code.
​ To learn more, see the sources I considered: [[1] Code Tool](https://docs.retellai.com/build/single-multi-prompt/code-tool) ​
Feel free to tag @AI Evy with additional questions. I also appreciate feedback, it helps me improve.