|
| 1 | +# Anthropic Tool Calling Conventions |
| 2 | + |
| 3 | +## Overview |
| 4 | + |
| 5 | +Tool calling in the ComputerUseAgent leverages Anthropic's Claude 3 API for executing system operations through well-defined tool interfaces. This document outlines the conventions and best practices for implementing and using tool calls with Anthropic's API. |
| 6 | + |
| 7 | +## Key Components |
| 8 | + |
| 9 | +- Tool Definitions: JSON Schema format specifications |
| 10 | +- Tool Results: Structured response handling |
| 11 | +- Chain of Thought: Explicit reasoning process |
| 12 | +- Error Handling: Standardized error reporting |
| 13 | + |
| 14 | +## Core Conventions |
| 15 | + |
| 16 | +### Tool Definition Structure |
| 17 | + |
| 18 | +```json |
| 19 | +{ |
| 20 | + "name": "tool_name", |
| 21 | + "description": "Detailed description of the tool's purpose and behavior", |
| 22 | + "input_schema": { |
| 23 | + "type": "object", |
| 24 | + "properties": { |
| 25 | + "param1": { |
| 26 | + "type": "string", |
| 27 | + "description": "Description of parameter 1" |
| 28 | + } |
| 29 | + }, |
| 30 | + "required": ["param1"] |
| 31 | + } |
| 32 | +} |
| 33 | +``` |
| 34 | + |
| 35 | +### Tool Description Guidelines |
| 36 | + |
| 37 | +1. Must include: |
| 38 | + - Purpose and primary use cases |
| 39 | + - Parameter explanations |
| 40 | + - Usage limitations |
| 41 | + - Expected output format |
| 42 | + - Error conditions |
| 43 | + |
| 44 | +2. Format: |
| 45 | + - Minimum 3-4 sentences |
| 46 | + - Clear, concise language |
| 47 | + - Explicit parameter requirements |
| 48 | + - Example usage scenarios |
| 49 | + |
| 50 | +### Tool Response Handling |
| 51 | + |
| 52 | +Tool responses should follow this structure: |
| 53 | +```json |
| 54 | +{ |
| 55 | + "role": "user", |
| 56 | + "content": [ |
| 57 | + { |
| 58 | + "type": "tool_result", |
| 59 | + "tool_use_id": "unique_id", |
| 60 | + "content": "result_content", |
| 61 | + "is_error": false |
| 62 | + } |
| 63 | + ] |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +### Chain of Thought Implementation |
| 68 | + |
| 69 | +- Use <thinking> tags for reasoning steps |
| 70 | +- Include parameter validation logic |
| 71 | +- Document tool selection process |
| 72 | +- Handle missing parameters gracefully |
| 73 | + |
| 74 | +Example format: |
| 75 | +``` |
| 76 | +<thinking> |
| 77 | +1. Analyzing user request |
| 78 | +2. Validating required parameters |
| 79 | +3. Selecting appropriate tool |
| 80 | +4. Preparing tool call |
| 81 | +</thinking> |
| 82 | +``` |
| 83 | + |
| 84 | +### Error Handling |
| 85 | + |
| 86 | +- Tool execution errors must include: |
| 87 | + - Clear error message |
| 88 | + - Error context |
| 89 | + - Suggested resolution |
| 90 | + - is_error flag set to true |
| 91 | + |
| 92 | +Example error response: |
| 93 | +```json |
| 94 | +{ |
| 95 | + "type": "tool_result", |
| 96 | + "tool_use_id": "id", |
| 97 | + "content": "Error: Invalid parameter format", |
| 98 | + "is_error": true |
| 99 | +} |
| 100 | +``` |
| 101 | + |
| 102 | +## Best Practices |
| 103 | + |
| 104 | +1. Tool Definition: |
| 105 | + - Use descriptive tool names |
| 106 | + - Provide comprehensive descriptions |
| 107 | + - Include all required parameters |
| 108 | + - Document optional parameters |
| 109 | + - Specify parameter constraints |
| 110 | + |
| 111 | +2. Parameter Handling: |
| 112 | + - Validate all inputs |
| 113 | + - Handle missing parameters explicitly |
| 114 | + - Use clear parameter descriptions |
| 115 | + - Include parameter type constraints |
| 116 | + |
| 117 | +3. Response Processing: |
| 118 | + - Parse tool results carefully |
| 119 | + - Handle errors gracefully |
| 120 | + - Maintain conversation context |
| 121 | + - Track tool usage metrics |
| 122 | + |
| 123 | +4. Chain of Thought: |
| 124 | + - Document reasoning process |
| 125 | + - Validate assumptions |
| 126 | + - Handle edge cases |
| 127 | + - Provide clear explanations |
| 128 | + |
| 129 | +5. Error Management: |
| 130 | + - Use consistent error formats |
| 131 | + - Include actionable feedback |
| 132 | + - Log error details |
| 133 | + - Handle retries appropriately |
| 134 | + |
| 135 | +## Anti-Patterns |
| 136 | + |
| 137 | +- Avoid: |
| 138 | + - Incomplete tool descriptions |
| 139 | + - Ambiguous parameter names |
| 140 | + - Missing error handling |
| 141 | + - Implicit parameter requirements |
| 142 | + - Undocumented limitations |
| 143 | + - Skipping chain of thought |
| 144 | + - Ignoring tool results |
| 145 | + - Mixing tool contexts |
| 146 | + |
| 147 | +## Model-Specific Considerations |
| 148 | + |
| 149 | +### Claude 3 Opus |
| 150 | +- Always use chain of thought |
| 151 | +- Leverage detailed reasoning |
| 152 | +- Handle complex parameter inference |
| 153 | +- Utilize parallel tool calls when appropriate |
| 154 | + |
| 155 | +### Claude 3 Sonnet |
| 156 | +- Focus on direct tool calls |
| 157 | +- Provide explicit parameters |
| 158 | +- Limit inference complexity |
| 159 | +- Use sequential tool calls |
| 160 | + |
| 161 | +### Claude 3 Haiku |
| 162 | +- Keep tool definitions simple |
| 163 | +- Require explicit parameters |
| 164 | +- Avoid complex inference |
| 165 | +- Use single tool calls |
| 166 | + |
| 167 | +## Token Usage Guidelines |
| 168 | + |
| 169 | +- Track input tokens: |
| 170 | + - Tool definitions |
| 171 | + - System prompts |
| 172 | + - User messages |
| 173 | + - Tool parameters |
| 174 | + |
| 175 | +- Monitor output tokens: |
| 176 | + - Tool results |
| 177 | + - Chain of thought |
| 178 | + - Error messages |
| 179 | + - Response formatting |
| 180 | + |
| 181 | +Note: Token usage varies by model. Refer to pricing documentation for specific model costs. |
0 commit comments