When to use Code Tool
dv and metadata objects. If the LLM must compose arguments when it
calls the tool, use a custom function.Create a Code Tool
Add a Code Tool

Code in the function-type menu.
Set the name and description
- Name:
calculate_daily_repayment - Description:
Calculate the daily repayment amount after fin_amount, tenure_days, and interest_rate are available as dynamic variables.
Write JavaScript
dv or metadata and return a JSON-serializable
value. Retell serializes the return value to JSON, sends it to the LLM as
the tool result, and extracts any response variables from it (next step).Store fields as variables
daily_repayment to
daily_repayment.See Store response variables for nested and
array paths.Configure execution feedback
Update the agent prompt
Test the code
console.log() output appear below the
editor.Test values stay in the dashboard and don’t change the live agent. The
dashboard test doesn’t populate metadata, so metadata-dependent code must
handle an empty object during testing.JavaScript environment
The code runs inside a QuickJS sandbox. Top-levelawait is supported.
dv: dynamic variables
Read dynamic variables as properties of dv. All
values in dv are strings, including values stored by earlier tools.
dv.user_number, not {{user_number}}, inside JavaScript. Runtime values
such as call_id, chat_id, session_type, direction, user_number, and
agent_number are available when they apply to the current session.
dv inside JavaScript. {{...}}
template substitution isn’t available inside code.metadata: call or chat metadata
Read metadata passed when you create the phone call
or chat.
dv, they aren’t limited
to strings.
fetch(url, options): HTTP requests
fetch() supports HTTP and HTTPS URLs, request methods, string-valued headers,
and request bodies. The returned response supports status, ok,
statusText, url, headers.get(), text(), and json().
console.log(): test output
Use console.log() while testing. Logs appear in Run Code results. They
aren’t added to the live tool result or shown as Code Tool logs in call logs.
Runtime limits
Array, Date,
JSON, Map, Math, Promise, RegExp, and Set. Node.js modules, browser
DOM APIs, require, import, and Intl aren’t available.
Store response variables
Response variables copy fields from the full return value into dynamic variables. For this return value:null, Retell skips that variable without
failing the execution.
Response-variable extraction uses the full return value, even when the result
sent to the agent is capped at 15,000 characters.
Configuration
Use Code Tool safely
- Don’t put API keys, credentials, or sensitive tokens in code,
dv, ormetadata. - Use
fetch()for low-risk HTTP reads. Put authenticated requests and state-changing operations behind a custom function on your backend. - Don’t make critical routing or financial decisions depend on an unvalidated
return value. Handle missing and invalid
dvvalues explicitly.
FAQ
Can the LLM pass arguments directly to Code Tool?
Can the LLM pass arguments directly to Code Tool?
dv and metadata. Use a custom function when the LLM must fill arguments at call time. If you chain an Extract Dynamic Variable tool before Code Tool, the LLM can still skip that first tool; use a conversation flow when the sequence must be deterministic.Why does the agent say the waiting message twice?
Why does the agent say the waiting message twice?
Can I use npm packages or browser APIs?
Can I use npm packages or browser APIs?
require, import, Node.js modules, browser DOM APIs, or Intl. Use the available JavaScript built-ins and fetch().What happens if the code times out or throws an error?
What happens if the code times out or throws an error?
try/catch for expected errors and return a structured fallback such as { "ok": false, "error": "lookup failed" }.Can I use async/await?
Can I use async/await?
await is supported, including for fetch() calls.Can I write dynamic variables from inside the code?
Can I write dynamic variables from inside the code?
dv. The sandbox receives a copy of dv, so changes you make to it inside the code don’t persist. To write a value back for later turns, return it and map it under Store Fields as Variables.Is there a limit on the result size?
Is there a limit on the result size?

