Back to catalog / Agent workflows
Build a bounded calculator tool
Let an agent request arithmetic while keeping expression parsing and execution in a deterministic local function.
Goal
Let an agent request arithmetic while keeping expression parsing and execution in a deterministic local function.
Steps
- Define a calculator tool schema with explicit numeric operands and supported operators rather than accepting arbitrary code. [source03]
- Dispatch
tool_useblocks fromclient.messages.createto a parser that rejects names, imports, attributes, and oversized expressions. [source03] - Return a numeric result or a structured error as
tool_result; test divide-by-zero and malicious input without evaluating Pythoneval. [source03]
How to verify
Check that supported arithmetic returns the expected number, division by zero returns a structured error, and inputs containing imports, attributes, or names never reach Python eval.
Troubleshooting
If an unsupported expression is accepted, narrow the parser to the explicit operator allowlist and add a regression case; never fall back to eval.
Permissions
Prerequisites
- Python and Anthropic API key; use a vetted arithmetic parser or a narrow hand-written grammar.
Permissions
- Use approved test or anonymized data only; scope credentials to each workflow, and require explicit approval for external writes.
Limitations
Model output may be incomplete or wrong; API and beta behavior can change, so verify against the pinned source and current service behavior.
Pinned upstream source
- Repository
- anthropics/anthropic-cookbook
- File path
- tool_use/calculator_tool.ipynb
- Pinned commit
9c1e493ddd2b82df399d2a61b9c806c539e18be6- Source checked
- 2026-10-14 · Reviewed against the pinned upstream source
Repository metrics
These values describe the upstream repository, not popularity or likes for an individual skill or playbook.
- Stars
- Not collected
- Forks
- Not collected
- Observed at
- Not collected
No metrics snapshot is available yet.
License: MIT
Main topic
Take a coding Agent change from context to verification