Skip to main content
A tool is an async Python function decorated with @tool. The function signature is the schema the LLM sees, and there is no separate registration step.

Defining a tool

skills/check_balance/tools.py
tool and ToolContext come from rasa.calm_v2.tools.decorator, and ToolResult from rasa.calm_v2.tools.result. Requirements:
  • The function must be async.
  • @tool takes description as a keyword argument. @tool(description="…"). A bare @tool without it raises.
  • The return type is ToolResult.
  • Name the injected parameter context. The runtime passes it as the keyword argument context=, and leaves it out of the LLM’s schema.

How the schema is derived

Type mapping: The generated object sets additionalProperties: false.
Each generated property carries its type. Put the meaning of an argument into the tool’s description, or name the parameter so its purpose is obvious.

Where tools are discovered

For a skill skills/<id>/, in resolution order:
  1. Skill-local: skills/<id>/tools.py and skills/<id>/tools/*.py. Both locations are scanned; no declaration needed. A name defined in tools/*.py shadows the same name in tools.py (logged as a duplicate).
  2. Shared: tools/*.py at the agent root. Only tools the skill declares via import_tools are attached to it; an undeclared shared tool is not available to that skill at all.
First match wins; skill-local beats shared. __init__.py is skipped. When a tool module cannot be imported, the model load stops with a ToolLoadingError naming that file, so an import mistake surfaces at load rather than as a missing tool mid-conversation.

Importing shared project code

While tool modules are imported, the project root is on sys.path, so a lib/ package at the agent root is importable from any tool location:
Do this at module top level. The project root is only importable during tool loading, not later when the tool is dispatched.

ToolContext

The runtime constructs one ToolContext per invocation and injects it: context.model carries two read-only values that are fixed for the loaded model: llm_config, the same provider settings the orchestrator uses from integrations.yml, and references_index, the packaged knowledge index (or None when the project ships no reference files). Use it when a tool needs to run its own model call or query the same knowledge the agent searches.
send is a coroutine, so await it: await context.send("One moment…").
context.events is a fresh list, so appending to or removing from it does not touch conversation state. Memory is the only supported write path. If the user has already interrupted the turn (voice barge-in), send drops the message.

context.memory

For reads, use either:
  • a bare name as declared in memory.yml — resolved against the active skill’s schema first, then the project-root memory.yml
  • a fully-qualified name (<skill_id>.<entry> or project.<entry>) — same visibility as conditions / prompt memory: the active skill’s public and private fields, every other skill’s public fields, and project fields, minus the active skill’s access.deny_read
Do not write session. prefixes here (that form is for conditions). Cross-skill writes stay out of scope: set only accepts bare names for the active skill or project.
  • get returns None when the entry is unset, outside the readable set (for example another skill’s private field), or when the skill’s access.deny_read blocks it. It never raises.
  • set writes through immediately and raises MemoryWriteError when the entry is undeclared, deny_write blocks it, the field is immutable, or the name collides with the reserved system.* namespace.
Every key a tool writes must be declared in the skill’s memory.yml or the project-root memory.yml. rasa train rejects an undeclared write with undeclared_memory_write. This is the most common authoring failure. See the memory.yml reference.

ToolResult

  • llm_response: any JSON-serialisable value; this is what the LLM reads as the tool’s result. It is used for LLM-invoked tools; framework-invoked tools (ordered-block execute_tool: steps, post-write hooks) act through context.
  • A tool that only sends a message or writes memory returns a bare ToolResult().

Gating a tool

By default every skill-local tool is visible to the LLM whenever its skill is active. Add tool_constraints in the frontmatter to gate one behind a memory condition:
The tool stays out of the LLM’s schema until the condition holds, and the same check runs again before the function is called. See Conditions. Visibility rules differ slightly by tier:
  • A skill-local tool is visible unless it appears in tool_constraints with an unmet condition.
  • A shared tool must be listed in import_tools; a tool_constraints entry then gates it as well.

Built-in framework tools

These names are reserved. A builder tool that reuses one raises at model load. Two behaviors worth knowing:
  • Once search_knowledge has run in a turn, the tool list collapses to activate, search_knowledge, and cannot_help, plus resolve_tool_confirmation when a confirmation is pending. The skill’s own tools are withheld so a grounded answer does not re-drive the skill, and return on the next turn.
  • cannot_help is withheld until a search has run when a knowledge base exists, so the model cannot decline before retrieval could have answered.

Post-write hooks

A tool named run_after_setting_<entry> is an internal hook, not an LLM tool. It is never offered to the model. The runtime calls it after a top-level set_fields or correct write to that entry, so you can validate the value or derive state from it.
tools/card_hooks.py
  • Returning llm_response with a non-null error key rolls the triggering write back and surfaces the message to the LLM. {"error": None} is success.
  • Writes made inside a hook do not re-trigger hooks.
  • A hook in the agent-root tools/ folder is available to every skill without import_tools, because an llm_settable project field can be written from any skill.

Cancellation

A turn can end while a tool is still running, whether from a voice barge-in, a session timeout, or a dropped connection. The runtime raises asyncio.CancelledError inside the tool at whatever await it is suspended on. There is nothing to wire up. Only await points are interruptible; synchronous code between them runs to completion.

What happens to your tool

When your tool returns normally, the runtime records the call and advances the skill to its next step. When your tool is cancelled mid-await, it does neither. The step stays pending, so the same tool can run again on the next turn. Memory it already wrote with context.memory.set(...) is kept. That gap matters for a side-effecting tool: your backend may have been called successfully while the conversation holds no record of it.

Check before irreversible work

context.is_cancelled is True once the turn has been cancelled. Check it immediately before anything you cannot safely repeat, and between consecutive side effects:
Returning early is clean: the call is recorded and the step advances, with no further await for a cancellation to slip into.

Release resources on cancellation

If your tool holds a resource, catch asyncio.CancelledError, release it, and re-raise. The turn has already ended, so the runtime discards anything you return from the handler.

See also