An ESP32 board can start a call with a OneSay AI Agent by itself, for example when someone presses its button. This is a
pre-production preview: it needs ESP32 SDK v0.1.9 or later and a service that serves startDeviceAgentCall, and it has not yet been
verified on a board. ESP32 SDK v0.1.8 declares the two functions below but cannot make the call: both return
ONESAY_ERR_INVALID_ARG before any request, whatever your settings are. The call that a person starts is described in
Talk to an AI Agent on an ESP32-S3-BOX-3. On OneSay's service today, the
Agent does not hear the board in a call that a person starts (ESP32 SDK 0.1.9 and earlier), so use the call on this page. For who does what in any call, see
How an AI Agent talks through an ESP32 board.
The short version
The board makes one request to OneSay. The answer holds everything the board needs to join a LiveKit Room: its join material and its
microphone publication. The board then joins the Room and publishes its microphone through LiveKit's own ESP32 SDK, and OneSay sends
the Agent into the Room once the microphone is published. This is how a LiveKit client normally works: one server call for a token,
then join and publish. Joining and publishing are documented in
LiveKit's ESP32 SDK (client-sdk-esp32).
The board may do this only because the person who owns it allowed it in advance, with a standing Delegation. Firmware can be read and copied, so the board's own claim is never a permission: OneSay checks the Delegation, the owner and the owner's permissions on every start.
Before you start: the owner's standing Delegation
The Device's Human owner creates the standing Delegation once, with their own credentials, never with the board's:
- Create:
createStandingDelegation(POST /devices/{device_id}/standing-delegations), with the Organization, Project and Environment, the Device asdelegate_principal_id, and apurpose. - Revoke:
revokeStandingDelegation(POST /devices/{device_id}/standing-delegations/{delegation_id}/revoke). A call that is running ends: OneSay removes the board from its Room. Later starts are refused withdelegation_revoked.
The Delegation stays until the owner revokes it. It lets the board start Agent calls for its owner; the owner pays for them (see
"What a call costs"). The board still cannot call createAgentExecution or stop an Agent itself.
A Device without an owner. A developer's Device that no Human owns has nobody to create the Delegation as its owner. Then an
Organization owner who may manage the Device creates it: delegation.create, plus device.update and room.join on the Device's
Environment. The calls are paid from that Organization's allowance. A Device has at most one current standing Delegation per
Environment: when another member already holds the Device's Delegation, a create is refused with 409 idempotency_conflict. Any other
member who may manage the Device and holds delegation.revoke can revoke it, for example after its creator left the Organization, and
then create their own. OneSay checks the creator's role at every start, as for an owner.
The board itself must be ready too: its Device is activated, and the board's microphone Source was created after that. Creating a
Source for a Device that is not activated yet is refused with 409 resource_state_invalid, with a message that names the missing
step.
The start reads the board's newest capability report, which is current for 300 seconds after the board publishes it.
onesay_esp32_device_agent_call_start does not publish it: your firmware does, before the start, with
onesay_esp32_control_publish_capabilities. The SDK's examples (agent_call, box3_agent_call) do this for every call when your
firmware provides load_device_secret in onesay_agent_call_config_t: a callback of the same type as load_rtc_bearer that
writes the long-lived Device secret (the BOX-3 example reads dev_secret from NVS). The example publishes before the first start
attempt and then erases the secret; the attempts after a 202 reuse that report. With the optional report_published_us, storage
your firmware keeps across calls (the BOX-3 example uses a static variable), the example does not publish again within 240 seconds
after the report was last published successfully; with NULL it publishes before every call. The board prints
ONESAY_AGENT_CALL_REPORT report_ms=<ms> error=<n>, or ONESAY_AGENT_CALL_REPORT cached age_s=<s> when it reused the report;
start_ms does not include the time spent publishing.
Publishing is best effort: if it fails, the board still starts the call, and the service decides. The result line then carries
report_error (the SDK's error, for example 6 when the Device secret could not be loaded) and report_code (the service's public
code, when it answered one); server_error keeps naming the first failing step's code. Without load_device_secret the example
publishes nothing. Either way, if your Application declares required capabilities and the board has no current report, the start
is refused with 409 application_incompatible: check dev_secret and the board's network, then start a new call.
Switch device-initiated calls on for an Environment
Device-initiated calls are off by default, for every Environment. While they are off, every start is refused with 403
capability_not_enabled and nothing is created; the switch does not change calls that a person starts. On OneSay's service
today, the Agent does not hear the board in a call that a person starts (ESP32 SDK 0.1.9 and earlier).
The switch belongs to each Environment. In this preview you cannot turn it on yourself: there is no public API operation and no
Console page for it yet. To have device-initiated calls turned on for your Environment, contact OneSay. This section will name the API
operation or the Console page as soon as one is documented. Until the switch is on, a board in your Environment gets
capability_not_enabled and cannot talk to an Agent. On OneSay's service today, the Agent does not hear the board in a call that a person
starts (ESP32 SDK 0.1.9 and earlier); with 0.1.10 such a call still connects, but no one can start an Agent on the board's microphone.
Turning the switch off stops new starts. A call that is already running is not ended by the switch; it ends as described in "How a call ends".
Start a call on the board
The board calls startDeviceAgentCall (POST /devices/{device_id}/agent-calls) with its own Device access token, for its own Device
and Environment. The request names the Agent profile and the board's microphone Source:
| Field | Meaning |
|---|---|
agent_profile | The Agent to talk to, ^[a-z][a-z0-9._-]{0,63}$ |
source_id | The board's microphone Source, which the Agent will hear |
Idempotency-Key header | Required; a new key for each attempt (see the next section) |
With the ESP32 SDK this is two calls. The first makes the one request; the second joins and publishes, and makes no request to OneSay:
onesay_action_device_agent_call_start_input_t input = {0};
/* device_id and environment_id must be the Control context's own; the SDK refuses another Device. */
strcpy(input.device_id, control->device_id);
strcpy(input.environment_id, control->environment_id);
strcpy(input.agent_profile, "voice-assistant");
strcpy(input.source_id, microphone_source_id);
onesay_esp32_rtc_t *session = NULL;
onesay_err_t result = onesay_esp32_device_agent_call_start(control, bearer, bearer_length, &input, now_unix_ms(), &session);
if (result == ONESAY_OK) {
onesay_rtc_audio_config_t audio = {0};
strcpy(audio.source_id, microphone_source_id); /* the started Source; leave delegation_id empty */
/* capture, render and the clock as for any call; require_echo_cancelled = true for a conversation */
result = onesay_esp32_device_agent_call_connect_audio(session, &audio);
}
/* ... the conversation ... */
onesay_esp32_rtc_close(session); /* leaving the Room ends the call */onesay_esp32_device_agent_call_start makes exactly one request and never retries by itself. On success the session holds the
admitted publication; join it with onesay_esp32_device_agent_call_connect_audio. A failed or refused start still needs
onesay_esp32_rtc_close if a session was returned.
The SDK's examples show the whole call. In agent_call, a non-empty device_agent_profile and source_id select the
device-initiated mode. On the ESP32-S3-BOX-3 example, write the profile to the NVS key agent_prof and the Source to src_id; the
board then starts the call itself and needs no Room, AppSession or connection Delegation from a person.
The two functions
Both are declared in onesay_esp32_rtc.h and work from ESP32 SDK v0.1.9:
onesay_err_t onesay_esp32_device_agent_call_start(onesay_esp32_control_context_t *control,
const uint8_t *bearer, size_t bearer_length,
const onesay_action_device_agent_call_start_input_t *input, uint64_t now_unix_ms,
onesay_esp32_rtc_t **output);
onesay_err_t onesay_esp32_device_agent_call_connect_audio(onesay_esp32_rtc_t *session,
const onesay_rtc_audio_config_t *config);onesay_esp32_device_agent_call_start sends one startDeviceAgentCall:
beareris the Device access token of this board.input->device_idandinput->environment_idmust be the Control context's own; the SDK refuses any other Device before a request is sent.input->agent_profilemust match^[a-z][a-z0-9._-]{0,63}$, andinput->source_idmust be this board's own active microphone Source; a value in the wrong format is refused before the request.now_unix_msis the board's clock. From SDK 0.1.10 the SDK does not use it to judge the join material: like LiveKit's own SDKs, it sends the material and the service judges its time with the service's clock. Up to 0.1.9 the SDK refused material that, by the board's clock, expired more than 65 s ahead or had already expired (60 s material), so a board more than about 5 s behind the service failed withONESAY_ERR_INVALID_ARG; with those versions, start the call only after SNTP has set the clock. OneSay does not trust the board's clock for anything else.- The idempotency key comes from the Control context's
next_idempotency_keycallback, once per call of this function. - On
ONESAY_OK,*outputis a prepared session that holds the admitted microphone publication.
onesay_esp32_device_agent_call_connect_audio joins the Room and publishes the microphone through LiveKit's own connect and track
publish. It makes one native connect attempt and no request to OneSay:
config->source_idmust be the Source the call was started with, andconfig->delegation_idmust be empty.- The capture and render callbacks, the clock and
require_echo_cancelledare the same as for any call (onesay_esp32_rtc_connect_audio); setrequire_echo_cancelledfor a conversation. - It refuses a session from
onesay_esp32_rtc_prepare, andonesay_esp32_rtc_connect_audiorefuses a started session, so a call never gets a second microphone publication. - Success means LiveKit accepted the connect, not yet that audio flows. Close the session with
onesay_esp32_rtc_closein every case.
On the ESP32-S3-BOX-3 example: one NVS row
In the BOX-3 example, add one row to your nvs.csv:
agent_prof,data,string,assistantThe value is the Agent profile (assistant is an example; use the profile of your Application). With agent_prof the board starts
the call itself: room_id, rtc_app_sess, rtc_conn_dlg and key_rtc_conn are not needed and are not read, and nothing is
started on a computer. Keep src_id (the microphone Source) and the other rows as they are. Build the NVS image with make_nvs.py,
write it to the nvs partition as for any change of the settings, and reset the board. When the start succeeds, the board prints one
line without any ids:
ONESAY_AGENT_CALL_START start_ms=<n> attempts=<n>start_ms is the time from the first request to the answer, in milliseconds; attempts is the number of starts it took.
Without agent_prof, the example waits for a person to start the call. On OneSay's service today, the Agent does not hear the board in a call that a person starts (ESP32 SDK 0.1.9 and earlier).
On the BOX-3 example with agent_prof, press BOOT to hang up a call: the board prints its result line and then
ONESAY_BOARD_NEXT_CALL press BOOT to start the next call, and the next press starts the next call with a new key. A call that a
person starts runs once per boot. When a start fails, the board's ONESAY_BOARD_CONFIG line and the result line's server_error
say why; the BOX-3 quickstart lists the error numbers.
When the answer is 202
A cold Room can take a moment to prepare. Until it is ready, OneSay answers 202 with state: preparing and retry_after_ms
(250 to 5,000 ms). What to do:
- Wait. The ESP32 SDK returns
ONESAY_ERR_RESOURCE_BUSY. From SDK 0.1.9,onesay_esp32_control_last_retry_after_ms(control)then gives the server'sretry_after_ms(0 when the answer carried none), and the examples wait that long first, else 100 ms; each later wait doubles, up to 5 s, plus up to 100 ms of jitter, in the shape of LiveKit's backoff. - Call
startDeviceAgentCallagain with a newIdempotency-Key. OneSay continues the board's one unfinished call; it does not start a second one.
The example's loop, shortened:
uint32_t wait_ms = 100; /* ONESAY_AGENT_CALL_START_RETRY_MS */
for (uint32_t attempt = 1; ; attempt++) { /* at most 9 attempts: ONESAY_AGENT_CALL_START_ATTEMPTS */
result = onesay_esp32_device_agent_call_start(control, bearer, bearer_length, &input, now_unix_ms(), &session);
if (result == ONESAY_OK || attempt >= 9) break;
if (result != ONESAY_ERR_RESOURCE_BUSY && !retryable_5xx(control)) break; /* a refusal: stop */
if (attempt == 1 && onesay_esp32_control_last_retry_after_ms(control) != 0) /* SDK 0.1.9: the server's hint */
wait_ms = onesay_esp32_control_last_retry_after_ms(control);
wait(wait_ms + jitter_0_to_100_ms()); /* for example 250 ms, 500 ms, 1 s, ... then 5 s */
wait_ms = wait_ms * 2 > 5000 ? 5000 : wait_ms * 2; /* ONESAY_AGENT_CALL_PREPARE_RETRY_MAX_MS */
}Each turn of the loop calls onesay_esp32_device_agent_call_start again, so next_idempotency_key gives each attempt a new key.
The same key only replays the stored 202: OneSay keeps the answer of each key, so repeating a key returns that answer again and never
moves the call forward. Give every attempt its own key; in the examples, the key callback (next_idempotency_key) returns a new key
for every startDeviceAgentCall. After a retryable 5xx, call again in the same way. Keep the number of attempts bounded: the
examples make at most 9 starts.
| Answer | What it means | What the board does |
|---|---|---|
201 | The Room, the board's AppSession, its connection with the LiveKit join material, and its microphone publication | Join and publish |
202 | The Room is still being prepared; nothing to join yet | Wait, then call again with a new key |
503 with a retryable code | OneSay could not check the authority just now | Wait, then call again |
| Any other refusal | Final for this start | Stop; see the table below |
Refusals
A refused start keeps OneSay's code in control->last_error.code.
| Code | HTTP | Meaning | What to do |
|---|---|---|---|
delegation_required | 403 | The board has no current standing Delegation | The owner creates one |
delegation_revoked | 403 | The owner revoked the standing Delegation | The owner creates a new one, if they want the board to start calls again |
grant_missing | 403 | The token is not a Device access token (details.required_principal_kind is device), the Device is not active or not the owner's, the owner left the Organization or lacks the permission, or the request names another call's Room. On a later start of a call: the Device's room.join Grant that the call was started under, or livekit.room_join, no longer holds, or the Application is not active or no longer covers the Environment | Use the board's own Device access token; check the Device, its Grants, its owner and the owner's role. If the call's own Grant is gone, end the call and start a new one |
resource_owner_mismatch | 403 | The device_id in the path is not the Device this token belongs to | Use the token's own Device ID |
capability_not_enabled | 403 | Device-initiated calls are not switched on for this Environment | Switch them on for the Environment (see "Switch device-initiated calls on for an Environment") |
resource_not_found | 404 | source_id is not this Device's own active microphone Source (details.resource is source) | Use the board's own microphone Source |
budget_exceeded | 429 | The owner's account cannot pay for the Agent: its allowance is used up, or the owner is a personal account, which device-initiated calls do not support yet | Top up or raise the allowance; nothing was created |
token_invalid | 401 | The Device access token is not valid | Get a new Device access token |
message_invalid | 400 | The body does not match the request (for example the agent_profile or source_id format) | Fix the request |
consent_request_mismatch | 409 | The Idempotency-Key was already used for a different request, or the Device's livekit.* Grants have changed since this key's answer was stored | Use a new key |
resource_state_invalid, capability_evidence_invalid | 409 | The stored answer for this key cannot be replayed | Use a new key |
quota_exceeded | 429 | An earlier call still holds this Environment's slot | End what is still open, then start again |
capability_authority_unavailable | 503 | OneSay could not check the authority just now (retryable) | Wait, then call again |
What a call costs
A device-initiated call is billed by usage, like a call that a person starts, and it counts as the usage of the owner who created the standing Delegation. When the Agent starts, OneSay reserves an amount of money for it, the admission hold. Each reply is charged against that hold as the call goes on; nothing is renewed in the middle of the call. The Agent ends when the reserved money is used up. When the call ends, only what was used is charged and the rest of the hold is released, so hanging up early costs only the replies so far. The board's connection time is counted as usage; it does not limit the call.
If the owner's account cannot pay for an Agent when the board starts a call, the start is refused with budget_exceeded and nothing
is created.
How a call ends
- The board leaves the Room (
onesay_esp32_rtc_close), or someone hangs up. - The money reserved for the Agent runs out.
- The owner revokes the standing Delegation: OneSay removes the board from the Room through LiveKit.
- The connection drops more than 6 hours after the start: the board cannot rejoin the call then, because its join window has closed, so it starts a new one. No time limit ends a connected call.
- On the BOX-3 example with
agent_prof, the BOOT button hangs up.
When OneSay removed the board, read why: see the next section.
Why the call ended
When OneSay ends a call, for example because the money ran out or the owner revoked the standing Delegation, it removes the board
from the Room through LiveKit. LiveKit's leave carries no reason of its own: the board only sees that it was removed
(Participant Removed, read with onesay_esp32_rtc_read_failure), and sometimes only that its connection was lost. To learn why, the board reads its call from OneSay once:
getDeviceAgentCall (GET /devices/{device_id}/agent-calls), with its own Device access token. The read needs no body, no
idempotency key and no Delegation, and it works after a revocation too.
The answer holds the board's newest call, or an empty list when it has none: the call's state (starting, ready, executing or
ended) and, once it has ended, end_reason. It never carries join material. Only the Device itself may read; any other caller is
refused.
With the ESP32 SDK this is one call:
onesay_device_agent_call_t call = {0};
onesay_err_t result = onesay_esp32_device_agent_call_read(control, bearer, bearer_length, control->device_id, &call);
if (result == ONESAY_OK && call.state == ONESAY_DEVICE_AGENT_CALL_ENDED) {
/* call.end_reason: what to tell the person at the board, from the table below */
}onesay_esp32_device_agent_call_read makes one request and never retries. It returns only the state and the reason; the call and
Room IDs are checked and never returned. An answer with another field, a second call or an unknown value is
ONESAY_ERR_INVALID_ARG, and a refused read keeps OneSay's code in control->last_error.code.
end_reason | What it means | What to do |
|---|---|---|
budget_exceeded | The owner's account could not pay for the Agent when it was about to join; the Agent never spoke (a call that reaches the money limit later reads ended) | Top up or raise the allowance, then start a new call |
quota_exceeded | An earlier call or Room still holds this Environment's slot | End what is still open, then start again |
delegation_revoked | The owner revoked the standing Delegation | The owner creates a new one, if the board should start calls again |
delegation_required | The board had no current standing Delegation when the Agent was started | The owner creates one, then start a new call |
grant_missing | The owner, the Device or the owner's permissions no longer allow the call | Check the Device, its owner and the owner's role |
horizon | The call ended more than 6 hours after it started (the 6 hours do not end a call; after them the board cannot rejoin it) | Start a new call |
not_joined (ESP32 SDK v0.1.9; not sent yet, see below) | The board never joined the call: its first answer expired before it joined (for example, the board restarted), or the Room closed with nobody in it | Start a new call; it gets a new Room |
ended | The call ended for another reason, for example the Agent finished | Start a new call when you want to talk again |
Which reasons a board can receive depends on the version of OneSay's interface it was built for. ESP32 SDK v0.1.9 is
built for the first version. For it a call the board never joined reads ended, because OneSay sends not_joined only to boards
built for a later version; v0.1.9 already decodes not_joined for when it is sent.
After a restart: if the board restarts during a call, LiveKit still counts it in the Room for up to about 20 seconds. A start in
that time returns the old call. Its join material has expired, so the SDK refuses it at decode or prepare
(ONESAY_ERR_INVALID_ARG), and nothing is charged. Start again every 5 seconds for up to 40 seconds, then report the failure.
After OneSay removes the board, its call can be recorded as ended a moment later. A read at that moment answers the
unfinished state without end_reason: treat it as ended.
The SDK's examples do this read once after every end of a call they started that the board did not cause itself: LiveKit ended the
call, whatever its reason (for example Participant Removed or Room Closed), or the connection was lost for good. They add
call_state and end_reason to their result line. A call that ends with the button, at its longest length, on an idle Agent or on a
media failure reads nothing.
Upgrade from ESP32 SDK v0.1.7
The device-initiated call needs ESP32 SDK v0.1.9. When you move a project from v0.1.7 to it, do all five steps:
- Use v0.1.9 with the service that serves it. v0.1.9 is built for a newer version of OneSay's interface. From the service update
that serves
startDeviceAgentCall, the service refuses an SDK built for the earlier version with 409capability_registry_mismatch, so a board on v0.1.7 must be updated. Use JavaScript SDK v0.1.8 or later with it for any tool on your computer (v0.1.12 is current; it follows the same contract revision). - Clone v0.1.9 into a new folder and build from scratch. Do not update the older folder in place, and do not reuse its
build folder,
sdkconfigordependencies.lock. The values ofonesay_delegation_workspace_kind_tchanged after v0.1.7 (a value was inserted and two were added), so rebuild everything against the v0.1.9 headers, and never store these values in flash or send them anywhere. - Set the custom environment's URLs before the build. If your configuration selects
ONESAY_ENVIRONMENT_CUSTOM, then beforeidf.py set-target, set insdkconfig.defaultsbothCONFIG_ONESAY_CUSTOM_CONTROL_API_URL(https://...) andCONFIG_ONESAY_CUSTOM_REALTIME_URL(wss://...). In v0.1.9 a build without them stops with a message that names both settings (CONFIG_ONESAY_CUSTOM_ENDPOINTS_CHECK, on by default); in v0.1.7 the board stopped only at boot. Firmware that never selects the custom environment can setCONFIG_ONESAY_CUSTOM_ENDPOINTS_CHECK=n. - Flash the new build. Your NVS settings (
nvs.csv) stay valid. A call that a person starts still connects. On OneSay's service today, the Agent does not hear the board in a call that a person starts (ESP32 SDK 0.1.9 and earlier): do step 5 too. - Let the board start calls. Add the two functions above to your firmware, or the
agent_profrow on the BOX-3 example, after the owner has created the standing Delegation and device-initiated calls are switched on for the Environment. After a removal, read why withonesay_esp32_device_agent_call_read.
A project already on v0.1.8 needs steps 2 to 5.
What has been verified
The request, the 202 and the refusals have been tested against OneSay's service in local integration tests, and the SDK examples build. The device-initiated call has not yet run on a board. Treat this page as a preview of how it works, and report what you see.
Next
- Talk to an AI Agent on an ESP32-S3-BOX-3
- How an AI Agent talks through an ESP32 board
- API Reference:
startDeviceAgentCall,createStandingDelegation,revokeStandingDelegation,getDeviceAgentCall