Docs Agents and tools
Patterns that keep an agent's speech calls cheap, attributable and easy to retry.
Why a gateway for agents
An agent will happily call a speech endpoint in a loop. The gateway makes that visible and bounded: a named key per agent, a hard budget, and a ledger that says which agent spent it. Without it, one runaway loop is indistinguishable from normal traffic until the machine is hot.
Give each agent its own key
| key name | agent |
|---|---|
nexa-bot · replies |
Short conversational lines, high rate, small budget |
nexa-bot · narration |
Long scripts, low rate, large budget |
content-desk · drafts |
Batch renders during the day |
Two keys, not one with a high limit: the failure of one agent should not starve the other.
Retry, correctly
import time, httpx
def speak(text: str, *, instructions: str, retries: int = 4) -> bytes:
body = {"input": text, "instructions": instructions, "response_format": "mp3"}
for attempt in range(retries):
response = httpx.post(
"https://tts.nexis-ai.tech/v1/audio/speech",
headers={"Authorization": f"Bearer {KEY}"},
json=body, timeout=180,
)
if response.status_code == 200:
return response.content
if response.status_code in (429, 503):
time.sleep(float(response.headers.get("Retry-After", 5)) * (attempt + 1))
continue
if response.status_code >= 500:
time.sleep(2 ** attempt)
continue
error = response.json().get("error", {})
raise RuntimeError(error.get("code"), error.get("message"))
raise RuntimeError("speech: retries exhausted")
Cache by content, not by call
The worker is deterministic for identical input + instructions + seed. Hash those three and store
the audio:
import hashlib, pathlib
digest = hashlib.sha256(f"{text}|{instructions}|{seed}".encode()).hexdigest()
path = pathlib.Path("cache") / f"{digest}.mp3"
if path.exists():
return path.read_bytes()
audio = speak(text, instructions=instructions)
path.write_bytes(audio)
A render worker that does this typically drops its character spend by half.
Split long scripts
3,000 characters is the ceiling per request. For a ten-minute voice-over:
- Split on sentence boundaries into 800–1,200 character chunks.
- Request them in order, retrying failures individually.
- Concatenate with
ffmpeg -f concat(mp3) orsox(wav).
Because the worker is single-stream, a serial loop is the correct client shape. Parallel calls only
produce 503 worker_busy.
Tool definition
A minimal tool schema a model can call:
{
"name": "speak",
"description": "Generate speech from text with the Nexis GPU worker.",
"parameters": {
"type": "object",
"required": ["input"],
"properties": {
"input": { "type": "string", "maxLength": 3000 },
"instructions": { "type": "string",
"description": "Voice description: age, accent, pace, warmth." },
"response_format": { "type": "string", "enum": ["mp3", "wav", "pcm"] }
}
}
}
Expose one tool with a narrow schema. A model that can choose between six speech tools will choose badly; one that can only name the text and the voice will not.
Budgeting an agent
Give the key what a bad day costs, not what a good day costs. If the agent's normal month is 40,000 characters, a 60,000 budget is generous and a 200,000 budget hides a loop for a week.
When a key hits quota_exceeded, the agent should degrade — skip optional audio, queue the rest, and
report the key name in its log line, not retry blindly.