Skip to content

examples: add tool use, multi-turn, thinking, and vision examples#22

Open
SNO7E-G wants to merge 1 commit intoanthropics:mainfrom
SNO7E-G:examples/tool-use-multi-turn-thinking-vision
Open

examples: add tool use, multi-turn, thinking, and vision examples#22
SNO7E-G wants to merge 1 commit intoanthropics:mainfrom
SNO7E-G:examples/tool-use-multi-turn-thinking-vision

Conversation

@SNO7E-G
Copy link
Copy Markdown

@SNO7E-G SNO7E-G commented Mar 25, 2026

Summary

This PR adds four missing fundamental API usage patterns to the examples/ directory. While the SDK supports these features natively, developers lacked clear, runnable reference implementations for how to orchestrate them in PHP.

Added Examples

  1. messages_tool_use.php — Demonstrates a complete agentic loop. It shows how to define tools via ToolUnionShape arrays, detect tool_use blocks in the response, execute functions locally, and feed the tool_result data back to the model. Note: The implementation pattern for appending $response->content to the history mirrors the SDK's internal BetaToolRunner approach.
  2. messages_multi_turn.php — Demonstrates conversation history accumulation, showing how to alternate user and assistant turns while preserving a system prompt across requests.
  3. messages_thinking.php — Demonstrates extended thinking mode. Crucially, it models both ThinkingConfigEnabled (with an explicit budget) and the recommended ThinkingConfigAdaptive approach, aligning directly with the SDK's deprecation warnings for non-adaptive thinking with Opus 4.
  4. messages_vision.php — Demonstrates vision capabilities using both URL-based (URLImageSource) and Base64-encoded (Base64ImageSource) image blocks mixed with text blocks.

Validation

  • ✅ All four files pass php -l syntax checks
  • ✅ Example usage models exactly align with MessagesService::create() signatures and BaseModel JSON serialization
  • ✅ Model constants are consistent with existing examples (using claude-sonnet-4-5-20250929)
  • ✅ Code style follows the same formatting conventions (shebang, autoloader placement, and PHPDoc usage) as existing examples like messages.php and messages_stream.php

These examples should significantly improve the onboarding experience for developers building modern agentic or multi-modal applications with the PHP SDK.

@SNO7E-G SNO7E-G requested a review from a team as a code owner March 25, 2026 07: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