Basic message sending. Reads user input and sends it to Claude, printing the response.
Streaming responses. Sends a request and prints text deltas as they arrive in real-time.
Tool/function calling. Defines a get_weather tool, handles tool_use responses, and sends tool results back to Claude.
Server-side tool search for working with large tool catalogs. Instead of loading all tool definitions upfront, Claude searches and loads only the tools it needs on-demand. This keeps context efficient and improves tool selection accuracy when you have many tools (30+).
The example defines several tools (weather, stock price, currency conversion, calculator, web search) with defer_loading: true, then uses tool_search_tool_regex to discover relevant tools at runtime.
Programmatic Tool Calling (PTC) allows Claude to write code that calls tools within a code execution container, rather than requiring round trips through the model for each tool invocation. This reduces latency for multi-tool workflows and decreases token consumption by allowing Claude to filter or process data before it reaches the context window.
The example defines a query_database tool with allowed_callers = ["code_execution_20250825"]. Claude then writes Python code that queries multiple database regions in a loop and aggregates the results—all in a single code execution, rather than multiple model round trips.
Requires the code_execution_20250825 tool.
Vision/image analysis. Sends a base64-encoded image to Claude and receives a description.
Structured outputs constrain Claude's responses to follow a specific schema, ensuring valid, parseable output for downstream processing.
The example demonstrates two features:
-
JSON outputs (
output_config): Extract structured data from text usingjsonSchemaConfigto guarantee valid JSON matching your schema. -
Strict tool use (
strict: true): Validate tool parameters usingstrictFunctionToolto guarantee schema-compliant tool inputs.
Prompt caching can be enabled directly from existing examples by setting cache_control on:
- top-level
CreateMessage - system blocks (
SystemPrompt/SystemBlock) - messages/content blocks
- tool definitions via
withToolCacheControl
Run any example with:
cabal run <example-name>