Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
added materials for How to Use the Claude API in Python for AI-Powere…
…d Applications
  • Loading branch information
nunombispo committed Mar 26, 2026
commit bcf7bfdf5526d794995721fe7d6bb4269326dd53
13 changes: 13 additions & 0 deletions how-to-use-claude-api-in-python/basic_claude_call.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import anthropic

client = anthropic.Anthropic()

response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "What is the Zen of Python?"}
],
)

print(response.content[0].text)
22 changes: 22 additions & 0 deletions how-to-use-claude-api-in-python/coding_assistant.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import anthropic

client = anthropic.Anthropic()

system_prompt = """
You are a Python coding assistant. You only answer questions about Python.
If the user asks about any other programming language or unrelated topic,
politely explain that you can only help with Python questions.
"""

user_input = input("Ask me anything about Python: ")

response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
system=system_prompt,
messages=[
{"role": "user", "content": user_input}
],
)

print(f"\n{response.content[0].text}")
2 changes: 2 additions & 0 deletions how-to-use-claude-api-in-python/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
anthropic
pydantic
38 changes: 38 additions & 0 deletions how-to-use-claude-api-in-python/structured_output_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import anthropic
import json

client = anthropic.Anthropic()

response = client.messages.create(
model="claude-sonnet-4-5",
max_tokens=1024,
system="You are a Python coding assistant.",
messages=[
{
"role": "user",
"content": "Write a Python function that adds two numbers.",
}
],
output_config={
"format": {
"type": "json_schema",
"schema": {
"type": "object",
"properties": {
"function_name": {"type": "string"},
"code": {"type": "string"},
"explanation": {"type": "string"},
},
"required": ["function_name", "code", "explanation"],
"additionalProperties": False,
},
}
},
)

result = json.loads(response.content[0].text)

print("--- Approach 1: Hand-written JSON schema ---")
print(f"Function: {result['function_name']}")
print(f"\nCode:\n{result['code']}")
print(f"\nExplanation: {result['explanation']}")
31 changes: 31 additions & 0 deletions how-to-use-claude-api-in-python/structured_output_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import anthropic

from pydantic import BaseModel


class FunctionDescription(BaseModel):
function_name: str
code: str
explanation: str

client = anthropic.Anthropic()

response = client.messages.parse(
model="claude-sonnet-4-5",
max_tokens=1024,
system="You are a Python coding assistant.",
messages=[
{
"role": "user",
"content": "Write a Python function that adds two numbers.",
}
],
output_format=FunctionDescription,
)

result = response.parsed_output

print("--- Approach 2: Pydantic + client.messages.parse() ---")
print(f"Function: {result.function_name}")
print(f"\nCode:\n{result.code}")
print(f"\nExplanation: {result.explanation}")
Loading