Tools¶
Decorator¶
tool ¶
tool(fn: Callable[P, R] | None = None, *, name: str | None = None, description: str | None = None, idempotent: bool = False) -> Tool | Callable[[Callable[P, R]], Tool]
Decorator to create a tool from a function.
Usage
@tool def search(query: str) -> str: '''Search the knowledge base.''' return "results..."
@tool(name="custom_name", description="Custom description") def my_tool(x: int) -> int: return x * 2
@tool(idempotent=True) def book_flight(flight_id: str, customer_id: str) -> dict: '''Book a flight — safe to mark idempotent because repeated calls with the same flight/customer would create duplicate bookings, which we never want.''' ...
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
fn
|
Callable[P, R] | None
|
The function to wrap |
None
|
name
|
str | None
|
Override tool name (defaults to function name) |
None
|
description
|
str | None
|
Override description (defaults to docstring) |
None
|
idempotent
|
bool
|
If True, the ReAct loop deduplicates calls with matching (name, arguments) within a single agent run. Prevents duplicate side-effects when a model re-issues a tool call it has already made this turn. |
False
|
Returns:
| Type | Description |
|---|---|
Tool | Callable[[Callable[P, R]], Tool]
|
Tool instance |
Source code in src/locus/tools/decorator.py
Tool class¶
Tool ¶
Bases: BaseModel
A tool that can be called by agents.
Created via the @tool decorator.
idempotent
class-attribute
instance-attribute
¶
When True, the ReAct loop deduplicates calls: if the model emits the same (tool_name, arguments) combination that has already been executed earlier in the current agent run, the prior result is reused and the tool function is not invoked again. Use for tools that either have side-effects you don't want duplicated (bookings, transfers, writes) or whose output is stable across the run (config/date lookups).
execute
async
¶
Execute the tool with given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
ctx
|
ToolContext | None
|
Optional tool context (injected if function accepts it) |
None
|
**kwargs
|
Any
|
Tool arguments |
{}
|
Returns:
| Type | Description |
|---|---|
Any
|
Tool result |
Source code in src/locus/tools/decorator.py
to_openai_schema ¶
Get OpenAI-compatible tool schema.
Source code in src/locus/tools/decorator.py
Tool context¶
ToolContext ¶
Bases: BaseModel
Context passed to tools during execution.
Provides access to agent state, metadata, and utilities.
get_metadata ¶
Built-in tools¶
get_today_date ¶
Return today's date plus common reference points for date arithmetic.
Call this whenever the user mentions a relative or partial date ("tomorrow", "next Monday", "in ten days", "April 20") so you can convert to an explicit YYYY-MM-DD before calling a date-sensitive tool.
Returns:
| Type | Description |
|---|---|
dict
|
A dict with: |
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|
dict
|
|