Tools
A method the model may call: its docstring, its arguments, confirmation, the hold melody, masking and timeouts.
A tool is a method the model may call. You write its description as a docstring and its arguments as the method's parameters; Pinecall builds the schema, runs the call and logs it.
What is a tool?#
A method decorated with @tool(). When the model calls it, the method runs in your process with
the arguments the model gave, and what it returns is what the model reads next.
/** Free slots on one day, for one specialty. Always call it for a day the patient names. */
@tool({ stage: ["choose", "book"], preview: 2 })
async freeSlots(day: string, specialty: string): Promise<Slot[]> {
const date = dayNamed(day, this.call.today ?? "");
if (!date) throw new NotADay(day);
this.slots = await this.agenda().free(date, specialty);
this.stage = this.slots.length > 0 ? "book" : "choose";
return this.slots;
}
@tool(stage=("choose", "book"), preview=2)
def free_slots(self, day: str, specialty: str) -> list[dict]:
"""Free slots on one day, for one specialty. Always call it for a day the patient names."""
date = day_named(day, self.call.today or "")
if not date:
raise NotADay(day)
self.slots = self.agenda.free(date, specialty)
self.stage = "book" if self.slots else "choose"
return self.slots
The docstring is the description, an Args: section describes each parameter, and the
annotations are the types: a Literal is an enum, a default makes a parameter optional. The
options are the same, as keywords: when=, stage=, confirm=, announce=, delayed=, delayed_after=, failed=, preview=, pii=,
timeout=.
A def tool runs on a thread, an async def one on the event loop.
# Free slots on one day, for one specialty. Always call it for a day the patient names.
tool stage: %i[choose book], preview: 2
def free_slots(day:, specialty:)
date = day_named(day, call.today.to_s)
raise NotADay, day unless date
self.slots = agenda.free(date, specialty)
self.stage = slots.any? ? :book : :choose
slots
end
The comment above tool is the description, and the keyword arguments are the parameters. The
options are the same: when:, stage:, confirm:, announce:, delayed:, delayed_after:, failed:, preview:, pii:, timeout:, and params:
for the types ({ day: String }).
How it works#
- The docstring is the description, and it is required. Without one no model can choose the
tool, so the class is refused before a call happens. It is read as one line: line breaks are
joined, and the first
@tagline ends it. - The signature is the schema. Parameter names and types become the JSON Schema the model fills.
string,number,booleanand arrays of them are typed; an interface or a type declared in the same file is read field by field; any other type becomes an open object. An optional parameter is not required. - The arguments are checked before the method runs. A missing required argument, or a value of the wrong JSON type, fails the call without running the method.
- Writes inside a tool are authored by the tool. The change log names it, every write of it on the call's log carries its name and the model's id for the call as its cause (State & visibility), and so does the console.
Options#
| option | what it does |
|---|---|
when: (state) => boolean |
shows the tool only while this returns true for the current state |
stage: "book" or ["choose", "book"] |
shows the tool only in these stages. See Stages |
confirm: "Booked: {{result.when}}." |
a sentence read to the caller after the tool runs, and it marks the tool irreversible |
announce: "Let me check the agenda." |
what the agent says as the tool starts, when the model's turn said nothing itself |
delayed: "Still checking." · delayedAfter: 3 |
what the agent says when the wait has grown long, 5 seconds in unless delayedAfter says otherwise |
failed: "I could not look that up." |
what the agent says when the tool failed, before the model reads why |
quiet: true |
a move on the caller's screen: it runs as the model calls it, while its words still play, and asks the model for no answer |
preview: 2 |
how many items of an array result the model sees. The state keeps them all |
pii: ["name", "phone"] |
parameters whose values the call's log masks wherever the agent wrote them down: personal data |
timeout: 8 |
seconds Pinecall waits for the method. 30 unless set, 300 at most |
params: z.object({ … }) |
an explicit zod schema, when the signature is not enough. Import z from @pinecall/agents: it is the zod the schema is read with |
when and stage together must both hold.
Confirmation#
confirm is a receipt. Once the tool has run, Pinecall fills the sentence and says it, word for
word, before the model replies. {{name}} is filled from an argument, and {{result.when}} from the
tool's return value:
@tool({ stage: "book", confirm: "Booked: {{result.when}} with {{result.professional}}." })
async book(slot: string): Promise<Booking> { … }
confirmdoes not hold the tool until the caller agrees. The tool has already run when the sentence is said, so write it as a receipt, never as a question like "Shall I book it?". Make the model ask before it calls the tool: say so in the docstring and in the view.
Announcing a tool#
A tool that takes a second or two leaves the caller in silence, or on the
hold melody. announce gives the agent a sentence to say as the tool starts:
@tool({ stage: "choose", announce: "Let me check the agenda." })
async freeSlots(day: string): Promise<Slot[]> { … }
It is said only when the model's turn said nothing itself. A model that answers "One moment, I will look at the agenda" and calls the tool in the same turn has already announced it, so the sentence stays unsaid: the caller never hears it twice. In a written chat it is a line of the agent's like any other.
On a spoken call, every tool the model sees, yours and the knowledge lookups, also carries an
optional argument say: one line the model writes for the wait, in the caller's language. When the
model writes it and its turn said nothing else, that line is said as the tool starts, in place of
announce. Your method never receives it, so no tool may declare an argument named say. A written
call's tools carry none: nobody waits in silence for a tool in a chat, and only an announce you
declared is written there.
A tool with confirm is declared irreversible to the gateway; every other tool is read. You do
not set this yourself.
The hold melody#
While a tool runs, a caller who hears nothing thinks the line dropped. Every agent plays a melody for that wait, with nothing to write in the class:
- When it plays. While a tool runs, while the model has not said its first word, and while the line is held. It starts once the agent has been quiet for the wait, 2.5 seconds unless you set another, so a tool or a model that answers sooner plays nothing. It stops at the answer.
- How it sounds. Looped under the agent's voice at 60 %, on the phone and on the web alike, and in the call's recording where it sounded. A written chat plays none.
- Which one. The runtime's own, until you upload a file of yours or turn it off.
It is one choice per agent per environment, set from the terminal or on the console's Pipeline screen, and the next call plays it:
pinecall pipeline melody # what plays, and after how long
pinecall pipeline melody upload espera.mp3 # a wav, mp3, ogg, flac or m4a, up to 20 MB and five minutes
pinecall pipeline melody after 1.5 # seconds of quiet before it starts, 0 to 30
pinecall pipeline melody after default # back to the runtime's 2.5
pinecall pipeline melody off # silence; `default` brings the runtime's back
Add --prod to set production's. Choosing what plays keeps the wait, and setting the wait keeps
what plays.
Or the class owns it: @agent({ hold: { file: "hold.mp3", afterS: 1.5 } }) (Python hold = {"file": "hold.mp3", "after_s": 1.5}, Ruby hold file: "hold.mp3", after_s: 1.5) names a file beside the
class, which the agent ships when it registers — on pinecall start and on a deploy, once, since
the gateway keeps the hash of what landed — and the wait before it; "off" is silence. Declared,
the Pipeline screen and pinecall pipeline melody show it locked, "set by the class", and a change
is a commit.
The wait counts from the agent's last word, so a tool with an
announceplays the sentence first and the melody only if the tool is still running after it. A model takes most of a second to its first word on almost every turn: a wait under a second plays the melody on ordinary turns too.
Personal data#
pii: ["name", "phone"] names arguments that carry personal data. The call's log keeps what the
model gave for them as ***, and masks the value, not only the argument, wherever the agent
wrote it down afterwards:
| entry in the log | what is masked |
|---|---|
tool.call |
the arguments you named, and the same value inside another argument |
state.changed |
the value inside whatever state field took it: message: { name: "***", … } |
tool.result |
the value inside what the tool answered, or inside its error |
| a line you logged | the value inside it |
A value is masked only where it stands as a whole word, so "Saved for Ana" reads "Saved for ***" and Banana stays Banana, and a value shorter than three characters is never hunted this
way, since masking 7 or ok would mask every such digit or word written after it (the argument
itself is still masked by its name). What was said is not touched. The caller's words (turn.user)
and the agent's replies (turn.agent) stay as they were spoken, because the log of a conversation
is that conversation: mask what the agent wrote down, not the transcript.
Your process still gets the real values: it runs the tool with them, and its state is its own. The
log keeps them sealed apart, and only the process that runs the call, one that takes it over and a
written call picked up again read them back. pinecall start prints the log's version, so a
terminal never shows what the log does not. A state field declared pii
(State & visibility) is masked whole by its name, with or
without a tool.
A tool that asks#
A tool often needs one more thing from the caller before it can finish: an email for the confirmation, a number to call back. A task asks for it in the middle of the tool.
import { Agent, GetEmail, agent, tool } from "@pinecall/agents";
@agent({ tasks: [GetEmail] })
export default class FrontDesk extends Agent {
/** Book a time, once the caller said yes to it. */
@tool()
async book(slot: string) {
const email = await this.ask(GetEmail);
return clinic.book(slot, email);
}
}
await this.ask(GetEmail) stops the tool there. The model is told what is missing and how to ask
for it, asks the caller, and gives the answer. The task checks it (an address that is none is asked
for again), and your tool goes on from the await with the value.
| task | what your tool gets |
|---|---|
GetEmail |
the address, lowercased |
GetPhone |
the digits, with the + of a country code |
GetName |
{ first, last } |
One of your own is task({ name, asks, how, fields, check }): what it asks for, how to ask, what
the model gives, and the check that returns what your tool is handed. A class names the tasks its
tools ask for in @agent({ tasks }).
Tasks are TypeScript's for now.
When a tool fails#
A tool that throws does not crash the call. The error's message becomes the tool's result, marked as an error, and the model reads it and can act on it. Throw a sentence a model can use, such as "Tuesday has no free slots", never a stack trace.
Ask the model for a word, not a record. book(slot: string), with the method looking the id up
among the slots it offered, is safer than book(slot: Slot). Asked for a whole object, a model fills
in one it was never offered.
Refused when the class loads#
Each of these stops the class before any call, with a message that says what to write. The
last is the gateway's, said when pinecall start registers the agent:
| mistake | message |
|---|---|
| no docstring | tool book: without a docstring no model can choose it; write a /** one line */ above the method |
| a name that is not one word | a tool name is one word a model can call, not … |
pii names a parameter the tool does not have |
tool book: pii names parameters the tool has; unknown: … |
stage on a class with no stage field |
tool book: stage names a value of this agent's own stage field, and … declares none |
@tool on something that is not a method |
@tool goes on a method; … is not one |
a tool that declares an argument named say |
bad_shape: tool echo: 'say' is the platform's argument, never a tool's own |