defineDevice when you want timeouts, retries, and group fan-out under version control in the agent project.
Verified against lua-cli 3.33.0.
Before you begin
- A device credential with the
commandsoperation. - The Node or Python client installed, or
lua-devicefromnpm install -g @lua-ai-global/device-clientfor thedefineDevicepath.
1
Declare the commands
A command has a
name matching ^[a-z][a-z0-9_]{0,63}$, a description the model reads as the tool description, and an optional JSON Schema inputSchema.- From the device
- With defineDevice
The list is sent at connect time; nothing is pushed. Change it and restart the device.
device.ts
2
Write the handlers
A handler receives the payload the model produced and returns the JSON the model gets back. A thrown error becomes
{ success: false, error: '<message>' }.device.ts
3
Check the tools the model has
While the device is online the agent has
device__label_printer__print_label, device__label_printer__paper_level, and device__label_printer__is_online; hyphens in the device name become underscores. Each command tool is described as [Device: label-printer] <description>. If the device is offline, this will return an error. When a self-describing device goes offline its command tools are removed on the next turn; a device declared with defineDevice keeps its tools, and calls return DEVICE_OFFLINE. Either way is_online stops answering { status: 'online' }.4
Read the results
A successful call returns the device’s reply as
{ commandId, success: true, data }. Failures come back as { success: false, error, message } with one of these error values.A handler that throws returns
success: false with the error message and is not retried.5
Verify
Send one command by hand and read the reply and latency; the command name is prompted for.
lua devices test runs the command regardless of which declaration path you used. Under --ci it exits 1 at the prompt.Options you may need
Retries and timeouts
timeoutMs sets how long the platform waits per attempt. retry: { maxAttempts, backoffMs } re-sends after a timeout or transport failure, waiting backoffMs × attempt between attempts; an offline or rate-limited device is never retried. Both fields work in the device’s command list and in defineDevice.
Group fan-out
Give severaldefineDevice declarations the same group and the agent also gets device__<group>__<command>__all, which sends the command to every registered device in the group and returns { total, succeeded, failed, results }; an offline member counts as failed. Membership is the group each device reports when it connects, so pass the same name to the client. Fan-out tools exist only for devices declared with defineDevice. To try a group without hardware, run the lua-device runner with a handlers file once per member. lua devices test sends one command to one connected device and can’t call the __all tool, and lua test has no device type, so exercise fan-out with lua chat -m.
Both declarations for one name
If a device connects with its own command list and adefineDevice declaration with the same name is pushed, the device’s list wins and the declaration’s commands are ignored for that device.
If it isn’t working
A command is missing from the agent's tools
A command is missing from the agent's tools
Its name doesn’t match
^[a-z][a-z0-9_]{0,63}$, its schema is over 4 KB, the device declared more than 128 commands, or the agent already has 128 device tools. Rename or trim, then reconnect.Every call returns DEVICE_OFFLINE
Every call returns DEVICE_OFFLINE
Run
lua devices status --device-name <name> --ci. offline or registered means the client isn’t connected; disabled means someone ran lua devices disable, so run lua devices enable --device-name <name>.Calls return TOO_MANY_REQUESTS
Calls return TOO_MANY_REQUESTS
The agent has 100 commands waiting on replies. Handlers that never return hold a slot until the timeout; return or throw promptly.
Next steps
Handle device triggers
The other direction: events from the device.
defineDevice reference
Every field of a command and trigger declaration.
lua devices
List, status, enable, disable, test.

