Skip to content

Add arithmetic calculator example using tool use#1252

Open
omid-ant wants to merge 1 commit intomainfrom
claude/add-arithmetic-calc
Open

Add arithmetic calculator example using tool use#1252
omid-ant wants to merge 1 commit intomainfrom
claude/add-arithmetic-calc

Conversation

@omid-ant
Copy link

Adds examples/arithmetic_calc.py — a self-contained example demonstrating how to build a simple arithmetic calculator using Claude's tool use feature.

The example defines four tools (add, subtract, multiply, divide) and runs an agentic loop that lets Claude chain multiple operations to evaluate an expression. Key design decisions:

  • Shared _AB_SCHEMA constant eliminates schema duplication across the three tools with identical signatures
  • Loop exits on any non-tool_use stop reason (handles end_turn, max_tokens, refusal, etc.) rather than only exiting on end_turn, avoiding an infinite loop on unexpected stop reasons
  • _run_tool handles divide-by-zero and unknown tool names explicitly
  • Follows existing example conventions (from anthropic import Anthropic, ToolParam/MessageParam type annotations)

@omid-ant omid-ant requested a review from a team as a code owner March 17, 2026 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant