Title: How to Use the Claude API in Python – Real Python
Open Graph Title: How to Use the Claude API in Python – Real Python
Description: Learn how to use the Claude API in Python to send prompts, control responses with system instructions, and get structured JSON output.
Open Graph Description: Learn how to use the Claude API in Python to send prompts, control responses with system instructions, and get structured JSON output.
Mail addresses
?subject=Python article for you&body=How to Use the Claude API in Python on Real Python
https://realpython.com/claude-api-python/
Opengraph URL: https://realpython.com/claude-api-python/
X: @realpython
Domain: realpython.com
{
"@context": "http://schema.org",
"@type": "Article",
"headline": "How to Use the Claude API in Python",
"image": {
"@type": "ImageObject",
"url": "https://files.realpython.com/media/How-to-Use-the-Claude-API-in-Python-for-AI-Powered-Applications_Watermarked.2bd1f7ed4e48.jpg",
"width": 1920,
"height": 1080
},
"mainEntityOfPage": {
"@type": "WebPage",
"@id": "https://realpython.com/claude-api-python/",
"lastReviewed": "2026-05-20",
"author": {
"@type": "Person",
"name": "Nuno Bispo",
"image": "https://realpython.com/cdn-cgi/image/width=1000,height=1000,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/nuno-bispo.f298f7795635.jpg",
"url": "https://realpython.com/team/nbispo/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"reviewedBy": [
{
"@type": "Person",
"name": "Aldren Santos",
"image": "https://realpython.com/cdn-cgi/image/width=500,height=500,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/Aldren_Santos_Real_Python.6b0861d8b841.png",
"url": "https://realpython.com/team/asantos/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Brenda Weleschuk",
"image": "https://realpython.com/cdn-cgi/image/width=320,height=320,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/IMG_3324_1.50b309355fc1.jpg",
"url": "https://realpython.com/team/bweleschuk/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Bartosz Zaczy\u0144ski",
"image": "https://realpython.com/cdn-cgi/image/width=1694,height=1694,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/coders_lab_2109368.259b1599fbee.jpg",
"url": "https://realpython.com/team/bzaczynski/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
{
"@type": "Person",
"name": "Philipp Acsany",
"image": "https://realpython.com/cdn-cgi/image/width=1920,height=1920,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/pacsany.19bcf16f5a00.jpg",
"url": "https://realpython.com/team/pacsany/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
}
]
},
"datePublished": "2026-05-20T14:00:00+00:00",
"dateModified": "2026-05-20T14:00:00+00:00",
"publisher": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": {
"@type": "ImageObject",
"url": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png",
"width": 512,
"height": 512
},
"description": "Real Python is a leading provider of online Python education and one of the largest language-specific online communities for software developers. It publishes high-quality learning resources, such as tutorials, books, and courses to an audience of millions of developers, data scientists, and machine learning engineers each month.",
"slogan": "Become a Python Expert",
"email": "info@realpython.com",
"sameAs": [
"https://github.com/realpython",
"https://www.youtube.com/realpython",
"https://twitter.com/realpython",
"https://x.com/realpython",
"https://www.linkedin.com/company/realpython-com/",
"https://www.facebook.com/learnrealpython",
"https://www.instagram.com/realpython",
"https://www.tiktok.com/@realpython.com"
]
},
"author": {
"@type": "Person",
"name": "Nuno Bispo",
"image": "https://realpython.com/cdn-cgi/image/width=1000,height=1000,fit=crop,gravity=auto,format=auto/https://files.realpython.com/media/nuno-bispo.f298f7795635.jpg",
"url": "https://realpython.com/team/nbispo/",
"affiliation": {
"@type": "Organization",
"@id": "https://realpython.com/#organization",
"name": "Real Python",
"url": "https://realpython.com",
"logo": "https://realpython.com/static/real-python-logo-square-512.157ae6bf64ed.png"
}
},
"description": "Learn how to use the Claude API in Python to send prompts, control responses with system instructions, and get structured JSON output.",
"hasPart": {
"@type": "FAQPage",
"mainEntity": [
{
"@type": "Question",
"name": "How do you call the Claude API in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003eInstall the \u003ccode\u003eanthropic\u003c/code\u003e package, set your \u003ccode\u003eANTHROPIC_API_KEY\u003c/code\u003e environment variable, and call \u003ccode\u003eclient.messages.create()\u003c/code\u003e with a \u003ccode\u003emodel\u003c/code\u003e name and a list of \u003ccode\u003emessages\u003c/code\u003e. The SDK reads the key automatically, so you don\u0026rsquo;t need to pass it explicitly in your script.\u003c/p\u003e"
}
},
{
"@type": "Question",
"name": "Is the Claude API free to use?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003eNo, the Claude API is paid and billed by \u003ca href=\"/ref/ai-coding-glossary/token/\" class=\"ref-link\"\u003etoken\u003c/a\u003e for both input and output. New Anthropic accounts can start with a small starter credit, and you can track spending in the \u003ca href=\"https://console.anthropic.com/\"\u003eClaude Console\u003c/a\u003e.\u003c/p\u003e"
}
},
{
"@type": "Question",
"name": "How do you get a Claude API key?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003eSign in to the Claude Console, open the API Keys section, and click \u003cem\u003eCreate Key\u003c/em\u003e. Copy the key right away because you won\u0026rsquo;t see it again, and store it as the \u003ccode\u003eANTHROPIC_API_KEY\u003c/code\u003e environment variable instead of pasting it into your code.\u003c/p\u003e"
}
},
{
"@type": "Question",
"name": "What\u2019s the difference between a system prompt and a user message in the Claude API?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003eA \u003ca href=\"/ref/ai-coding-glossary/system-prompt/\" class=\"ref-link\"\u003esystem prompt\u003c/a\u003e is passed as the top-level \u003ccode\u003esystem\u003c/code\u003e parameter on \u003ccode\u003eclient.messages.create()\u003c/code\u003e and shapes Claude\u0026rsquo;s role, tone, and constraints across the conversation. User messages live inside the \u003ccode\u003emessages\u003c/code\u003e list with the \u003ccode\u003e\"user\"\u003c/code\u003e role and represent the actual back-and-forth dialogue.\u003c/p\u003e"
}
},
{
"@type": "Question",
"name": "How do you get structured JSON output from the Claude API in Python?",
"acceptedAnswer": {
"@type": "Answer",
"text": "\u003cp\u003ePass an \u003ccode\u003eoutput_config\u003c/code\u003e with a JSON schema to \u003ccode\u003eclient.messages.create()\u003c/code\u003e, or use \u003ccode\u003eclient.messages.parse()\u003c/code\u003e with a Pydantic \u003ccode\u003eBaseModel\u003c/code\u003e to receive a typed Python object. Both approaches force Claude to return data that matches the shape you specified, which eliminates fragile string parsing.\u003c/p\u003e"
}
}
]
}
}
| author | Real Python |
| theme-color | #18354c |
| color-scheme | light dark |
| twitter:card | summary_large_image |
| twitter:image | https://files.realpython.com/media/How-to-Use-the-Claude-API-in-Python-for-AI-Powered-Applications_Watermarked.2bd1f7ed4e48.jpg |
| og:image | https://files.realpython.com/media/How-to-Use-the-Claude-API-in-Python-for-AI-Powered-Applications_Watermarked.2bd1f7ed4e48.jpg |
| twitter:creator | @realpython |
| og:type | article |
| og:site_name | Real Python |
| apple-mobile-web-app-title | Real Python |
Links:
Viewport: width=device-width, initial-scale=1, shrink-to-fit=no, viewport-fit=cover
Robots: max-image-preview:large