Skip to main content
  1. Posts/

How to Use the Claude API in Python

🤖 Using the Claude API in Python

Anthropic made Claude, their large language model, available through a REST API with an official Python SDK. With just a few lines of code, you can integrate conversational AI into your projects.

🚀 What can you do?

  • 📦 Install anthropic and authenticate with an API key stored as an environment variable
  • 💬 Send prompts with client.messages.create() and read the reply from response.content[0].text
  • 🎯 Control model behavior with system prompts — define the role, tone, and constraints before the conversation starts
  • 📊 Get structured JSON responses using a custom schema or Pydantic models

🔑 Key takeaways:

  • The system parameter shapes Claude before the user says a single word
  • max_tokens is a hard ceiling, not a target — set it high enough for the expected response
  • With output_config and a JSON Schema, Claude returns data ready to process without parsing free-form text

💡 Explanation in a nutshell
#

Think of Claude as a very smart assistant you can give written instructions to. The Claude API is the “door” that lets you talk to that assistant from your Python code. You send a question, it responds, and you can ask it to reply in a structured format (like a JSON object) so the result is easy to use in the rest of your application.

More information at the link 👇

Also published on LinkedIn.
Juan Pedro Bretti Mandarano
Author
Juan Pedro Bretti Mandarano