Skip to content

Latest commit

 

History

History

examples

README.md

Examples

claude-example

Basic message sending. Reads user input and sends it to Claude, printing the response.

claude-stream-example

Streaming responses. Sends a request and prints text deltas as they arrive in real-time.

claude-tool-example

Tool/function calling. Defines a get_weather tool, handles tool_use responses, and sends tool results back to Claude.

claude-tool-search-example

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.

claude-programmatic-tool-calling-example

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.

claude-vision-example

Vision/image analysis. Sends a base64-encoded image to Claude and receives a description.

claude-structured-outputs-example

Structured outputs constrain Claude's responses to follow a specific schema, ensuring valid, parseable output for downstream processing.

The example demonstrates two features:

  1. JSON outputs (output_config): Extract structured data from text using jsonSchemaConfig to guarantee valid JSON matching your schema.

  2. Strict tool use (strict: true): Validate tool parameters using strictFunctionTool to guarantee schema-compliant tool inputs.

Prompt caching support

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>