Tools that use the State¶
A tool can read or change the run it is part of.
from alpineagents import State, tool
@tool
def remember(key: str, value: str, state: State) -> str:
"""Save a note for later in this run"""
state.data.setdefault("notes", {})[key] = value
return f"Saved {key}"
@tool
def recall(key: str, state: State) -> str:
"""Read a note saved with remember"""
return state.data.get("notes", {}).get(key, f"No note named {key}")
- A parameter typed
Stateis hidden from the model. The model seesremember(key, value)andrecall(key). - When the tool runs, that parameter receives the current State.
state.datakeeps values across turns. The model never sees it.
What a tool can do with the State¶
| Call | Effect |
|---|---|
state.data[...] |
Read or keep your own values |
state.finish(answer) |
End the run after this turn. See Stop conditions |
state.add_notice(text) |
Tell the model something. It goes into the context right after this turn's results |
state.answer, state.turn, state.usage, ... |
Read the run |
Thread safety¶
Tools in one turn can run at the same time on worker threads. Every State method is thread-safe. For an update to
state.data in several steps, hold state.lock. See State: your own data.