Agent Wealth HubFree public resources
中文 EN العربية

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

  1. Define a calculator tool schema with explicit numeric operands and supported operators rather than accepting arbitrary code. [source03]
  2. Dispatch tool_use blocks from client.messages.create to a parser that rejects names, imports, attributes, and oversized expressions. [source03]
  3. Return a numeric result or a structured error as tool_result; test divide-by-zero and malicious input without evaluating Python eval. [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

Permissions

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

https://github.com/anthropics/anthropic-cookbook/blob/9c1e493ddd2b82df399d2a61b9c806c539e18be6/tool_use/calculator_tool.ipynb

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

Related resources