File: web_search.py

package info (click to toggle)
anthropic-sdk-python 0.76.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,432 kB
  • sloc: python: 30,183; sh: 186; makefile: 5
file content (35 lines) | stat: -rw-r--r-- 976 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
from __future__ import annotations

from anthropic import Anthropic

client = Anthropic()

# Create a message with web search enabled
message = client.messages.create(
    model="claude-sonnet-4-5-20250929",
    max_tokens=1024,
    messages=[{"role": "user", "content": "What's the weather in New York?"}],
    tools=[
        {
            "name": "web_search",
            "type": "web_search_20250305",
        }
    ],
)

# Print the full response
print("\nFull response:")
print(message.model_dump_json(indent=2))

# Extract and print the content
print("\nResponse content:")
for content_block in message.content:
    if content_block.type == "text":
        print(content_block.text)

# Print usage information
print("\nUsage statistics:")
print(f"Input tokens: {message.usage.input_tokens}")
print(f"Output tokens: {message.usage.output_tokens}")
if message.usage.server_tool_use:
    print(f"Web search requests: {message.usage.server_tool_use.web_search_requests}")