Alexander
05/13/2026, 12:09 PMAI Evy
05/13/2026, 12:10 PMAI Evy
05/13/2026, 12:10 PMfetch() 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:
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.AI Evy
05/13/2026, 12:10 PMAI Evy
05/13/2026, 12:10 PMAI Evy
05/13/2026, 12:10 PMAlexander
05/13/2026, 12:13 PMAI Evy
05/13/2026, 12:13 PMAI Evy
05/13/2026, 12:13 PMjavascript
// 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:
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 };AI Evy
05/13/2026, 12:13 PMdv 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.AI Evy
05/13/2026, 12:13 PMAI Evy
05/13/2026, 12:13 PMAlexander
05/13/2026, 12:14 PMAI Evy
05/13/2026, 12:14 PMAI Evy
05/13/2026, 12:14 PM{{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) ]()
json
{
"retell_llm_dynamic_variables": {
"zip_code": "97224",
"closest_clinic": "Tigard"
}
}
Then in your agent prompt, add:
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.AI Evy
05/13/2026, 12:14 PMAI Evy
05/13/2026, 12:14 PMAlexander
05/13/2026, 12:16 PMAI Evy
05/13/2026, 12:16 PMAI Evy
05/13/2026, 12:17 PMAI Evy
05/13/2026, 12:17 PMAI Evy
05/13/2026, 12:17 PMAlexander
05/13/2026, 12:18 PMAI Evy
05/13/2026, 12:18 PMAI Evy
05/13/2026, 12:18 PM{{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.AI Evy
05/13/2026, 12:18 PMAI Evy
05/13/2026, 12:18 PM