Skip to navigation

Add a before-call tool

Summary

A before-call tool is a webhook that the platform sends while an inbound call rings, and its response goes into the agent’s system prompt. This guide adds a before-call tool to an agent’s draft and publishes the agent. It does not cover tools that the model calls during the call; see Tools and MCP servers.

Facts

ItemValue
OperationsgetAgentDraft, createAgentTool, publishAgent
PathsGET /agents/{agent_id}/draft, POST /agents/{agent_id}/tools, POST /agents/{agent_id}/publish
Related operationslistAgentTools, validateAgentTool, replaceAgentTool, deleteAgentTool
Capabilityconfig.read for reads; tools.write for tool writes, and secrets.read for a secret header reference; agents.publish to publish
Required headersAuthorization on every request; Idempotency-Key and If-Match with the draft ETag on createAgentTool and publishAgent
Field that makes it a before-call tool"timing": "pre_call" in the tool body. An omitted timing on create is during_call
When it runsInbound calls only, one time while the phone rings. It does not run on outbound calls or browser test calls
Tools for each agentAt most one before-call tool
Kindwebhook only
Parameter sourceconstant or dynamic_variable. A model parameter is refused, because no model runs before the call
side_effect, retry_postureBoth none: a lookup with no automatic retry
deadline_ms500 to 10000; 3000 when omitted
max_response_bytesAt most 4096; 4096 when omitted
Omitted limitsTake the before-call defaults, not the during-call ones
wait_for_responsetrue
NameNot an agent variable, agent_name or language, and not starting with system__
Caller numberThe dynamic variable system__caller_id, as a dynamic_variable parameter value
Prompt placementAt the {{<tool name>}} placeholder in the system prompt, or at the end of the prompt without one
First messageOne field with a fallback: {{<tool name>.<path>|<fallback>}}
EnvironmentRASEN_API_BASE (the base URL, ending in /v1), RASEN_API_KEY, AGENT_ID, SECRET_ID, ETAG
Python3.10 or later, with the httpx package
TypeScriptNode.js 22 or later (built-in fetch)

Steps

Set AGENT_ID to the id of the agent that answers your inbound number. listAgents lists the agents of the workspace, and Route phone calls assigns an agent to a number. Set SECRET_ID to the id of a secret with purpose webhook_tool that holds the API key of your lookup service (Secrets and rotation).

1. Read the draft ETag

curl --fail-with-body -sS -i "$RASEN_API_BASE/agents/$AGENT_ID/draft" \
-H "Authorization: Bearer $RASEN_API_KEY"

Set ETAG to the ETag of the answer, including its quotes. It is the draft revision that the next write needs in If-Match.

2. Add the before-call tool

This tool sends the caller’s number to your lookup service as the phone query parameter, from the dynamic variable system__caller_id. It sends the API key of the service from the secret in the X-Api-Key header. It omits deadline_ms and max_response_bytes, so it takes the before-call defaults: 3000 ms and 4096 bytes.

curl --fail-with-body -sS -i -X POST "$RASEN_API_BASE/agents/$AGENT_ID/tools" \
-H "Authorization: Bearer $RASEN_API_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-H "If-Match: $ETAG" \
-H "Content-Type: application/json" \
-d '{
"name": "lookup_caller",
"description": "Look up the caller by phone number.",
"kind": "webhook",
"timing": "pre_call",
"side_effect": "none",
"retry_posture": "none",
"definition": {
"url": "https://api.example.com/callers",
"method": "GET",
"parameters": [
{"name": "phone", "location": "query", "source": "dynamic_variable",
"value": "system__caller_id", "schema": {"type": "string"}}
],
"headers": [
{"name": "X-Api-Key", "value_type": "secret",
"credential": {"secret_id": "'"$SECRET_ID"'", "version": 1}}
]
}
}'

The 201 answer is the tool. Its configuration has "timing": "pre_call", "deadline_ms": 3000 and "max_response_bytes": 4096. Set ETAG to the ETag of the answer: adding the tool changed the draft revision.

The response body of your service goes into the system prompt as data, so the agent knows the caller from its first sentence. Put the placeholder {{lookup_caller}} in the prompt where the data belongs; without it, the data goes at the end of the prompt. The first message can name one field of the response with a fallback, for example Hi {{lookup_caller.name|there}}. Edit the prompt and the first message as in Build and publish an agent.

3. Publish the agent

curl --fail-with-body -sS -X POST "$RASEN_API_BASE/agents/$AGENT_ID/publish" \
-H "Authorization: Bearer $RASEN_API_KEY" \
-H "Idempotency-Key: $(uuidgen)" \
-H "If-Match: $ETAG"

The tool runs on inbound calls that start after the publish. Calls already admitted keep their version. The version’s tools list holds the before-call tool with "timing": "pre_call".

Rules

  • MUST send "timing": "pre_call" to make a before-call tool. An omitted timing on create makes a during-call tool; an omitted timing on replace keeps the stored timing.
  • A replace that sends timing moves the tool between during_call and pre_call. The tool MUST then meet the rules of its new timing.
  • MUST NOT send timing on an MCP tool write: only a webhook tool runs before the call. The API refuses it with 400 invalid_request.
  • MUST NOT give a before-call tool a parameter with source model.
  • MUST send side_effect none and retry_posture none. The platform sends the request one time and never retries it.
  • MUST keep deadline_ms from 500 to 10000 and max_response_bytes at most 4096. A value outside 100 to 30000 ms or 256 to 65536 bytes is 400 invalid_request for every tool, before the before-call bounds apply.
  • MUST NOT name the tool after an agent variable, agent_name or language, or start its name with system__.
  • MUST NOT send a body from a GET or DELETE tool. Put its values in the query or the path.
  • MUST send "timing": "pre_call" to validateAgentTool to check a before-call tool. An omitted timing is checked as a during-call tool.
  • A slow or failed request does not stop the call: the agent answers without the data.

Verify

curl --fail-with-body -sS "$RASEN_API_BASE/agents/$AGENT_ID/tools" \
-H "Authorization: Bearer $RASEN_API_KEY"

Expected output: the tool from step 2, with "timing": "pre_call" in its configuration.

{"items": [{"id": "tool_01j00000000000000000000000", "agent_id": "agent_01j00000000000000000000000", "configuration": {"name": "lookup_caller", "kind": "webhook", "timing": "pre_call", "side_effect": "none", "retry_posture": "none", "deadline_ms": 3000, "max_response_bytes": 4096}}], "next_cursor": null}

After an inbound call, Read tool call logs shows the run of the tool with "timing": "pre_call" and no model arguments.

Errors

CodeCauseFix
tool_pre_call_existsThe agent already has a before-call tool.Replace or delete that tool, or send "timing": "during_call".
tool_pre_call_llm_parameterA parameter has source model. details.index names the parameter.Use constant or dynamic_variable.
tool_pre_call_side_effectside_effect or retry_posture is not none. details.field names the field.Send none for both.
tool_pre_call_boundsdeadline_ms, max_response_bytes or wait_for_response is outside the before-call bounds. details.field names it, with details.minimum and details.maximum.Send a deadline_ms from 500 to 10000, a max_response_bytes of at most 4096 and wait_for_response true.
tool_pre_call_nameThe name is an agent variable, agent_name or language, or starts with system__. On publishAgent, a variable of the draft has the tool’s name.Rename the tool or the variable.
tool_body_on_get_or_deleteA GET or DELETE tool has body parameters or puts its effect ID in the body. On publishAgent, details.name names the tool.Move the body values to the query or the path, and the effect ID to a header.
tool_name_conflictAnother tool of the draft has this name.Use another name.
draft_conflictThe draft changed after you read its ETag.Read the draft again (step 1), then send the request with the new ETag.
outbound_destination_refusedThe URL names a private address.Use a public HTTPS URL.
invalid_requestAn MCP tool write sent timing, or a limit is outside the bounds of every tool.Send timing only on a webhook tool. Keep deadline_ms from 100 to 30000 and max_response_bytes from 256 to 65536.
credential_unavailableThe secret version is revoked or destroyed, belongs to another workspace, or has another purpose.Use a releasable version of a webhook_tool secret.
forbiddenThe key lacks tools.write, secrets.read or agents.publish. details.required_capabilities lists them.Use a key with those capabilities.