How the loop works
- Your code sends the conversation and the Keenable tools to
client.messages.create. - If Claude wants the web, the response has
stop_reason: "tool_use"and onetool_useblock per call, each with aname, anidand a decodedinputobject. - Your code runs each call with
run_tool_calland sends every result back in oneusermessage, astool_resultblocks with the matchingtool_use_id. - When
stop_reasonis anything other thantool_use, the text blocks hold Claude’s answer.
Prerequisites
- An Anthropic API key in
ANTHROPIC_API_KEY. - Python 3 or Node.js with
tsxto run the program. - No Keenable key. The SDK searches without one. To raise the rate limit, set
KEENABLE_API_KEYto an API key.
Tool definition (copy verbatim)
Anthropic’s Messages API takes each tool asname, description and input_schema. These are the Keenable SDK’s definitions in that shape:
Anthropic Messages format
TOOLS at run time, so the definitions always match the SDK version you installed. If you paste the JSON instead, check the SDK column of the arguments table: an older run_tool_call drops any argument its version doesn’t offer.
Run the loop
Install the Keenable SDK and Anthropic’s SDK:claude-tools.mts or claude_tools.py. It asks a question, runs every tool call Claude makes, and prints the answer:
npx tsx claude-tools.mts or python claude_tools.py. The program prints Claude’s final answer. The question asks for sources, so the answer names the pages Claude read.
If a tool call fails, for example on a page that can’t be fetched, the tool_result block carries is_error: true and the error message, so Claude can try another URL or query. Errors inside a tool call lists what run_tool_call raises.