mirror of
https://github.com/anthropics/claude-code-sdk-python.git
synced 2025-07-07 14:45:00 +00:00
Initial Python SDK import
This commit is contained in:
parent
19c71ae9ca
commit
6ca3514261
22 changed files with 1774 additions and 1 deletions
77
examples/quick_start.py
Normal file
77
examples/quick_start.py
Normal file
|
@ -0,0 +1,77 @@
|
|||
#!/usr/bin/env python3
|
||||
"""Quick start example for Claude Code SDK."""
|
||||
|
||||
import anyio
|
||||
|
||||
from claude_code_sdk import (
|
||||
AssistantMessage,
|
||||
ClaudeCodeOptions,
|
||||
ResultMessage,
|
||||
TextBlock,
|
||||
query,
|
||||
)
|
||||
|
||||
|
||||
async def basic_example():
|
||||
"""Basic example - simple question."""
|
||||
print("=== Basic Example ===")
|
||||
|
||||
async for message in query(prompt="What is 2 + 2?"):
|
||||
if isinstance(message, AssistantMessage):
|
||||
for block in message.content:
|
||||
if isinstance(block, TextBlock):
|
||||
print(f"Claude: {block.text}")
|
||||
print()
|
||||
|
||||
|
||||
async def with_options_example():
|
||||
"""Example with custom options."""
|
||||
print("=== With Options Example ===")
|
||||
|
||||
options = ClaudeCodeOptions(
|
||||
system_prompt="You are a helpful assistant that explains things simply.",
|
||||
max_turns=1,
|
||||
)
|
||||
|
||||
async for message in query(
|
||||
prompt="Explain what Python is in one sentence.",
|
||||
options=options
|
||||
):
|
||||
if isinstance(message, AssistantMessage):
|
||||
for block in message.content:
|
||||
if isinstance(block, TextBlock):
|
||||
print(f"Claude: {block.text}")
|
||||
print()
|
||||
|
||||
|
||||
async def with_tools_example():
|
||||
"""Example using tools."""
|
||||
print("=== With Tools Example ===")
|
||||
|
||||
options = ClaudeCodeOptions(
|
||||
allowed_tools=["Read", "Write"],
|
||||
system_prompt="You are a helpful file assistant.",
|
||||
)
|
||||
|
||||
async for message in query(
|
||||
prompt="Create a file called hello.txt with 'Hello, World!' in it",
|
||||
options=options
|
||||
):
|
||||
if isinstance(message, AssistantMessage):
|
||||
for block in message.content:
|
||||
if isinstance(block, TextBlock):
|
||||
print(f"Claude: {block.text}")
|
||||
elif isinstance(message, ResultMessage) and message.cost_usd > 0:
|
||||
print(f"\nCost: ${message.cost_usd:.4f}")
|
||||
print()
|
||||
|
||||
|
||||
async def main():
|
||||
"""Run all examples."""
|
||||
await basic_example()
|
||||
await with_options_example()
|
||||
await with_tools_example()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
anyio.run(main)
|
Loading…
Add table
Add a link
Reference in a new issue