Claude API tutorial: a first Python script, choosing models, controlling cost and getting JSON back
The Claude API lets you put Claude inside your own scripts, apps and workflows. This tutorial covers getting an API key, a first Python call, system prompts, model choice, token costs, prompt caching, batch processing and reliable structured output.
The Claude API lets your own code send messages to Claude and get responses back, so you can automate document processing, build assistants into apps or add AI steps to workflows. Start with an API key from the Claude Console, the official Python SDK, a clear system prompt and the smallest model that does the job well; then add prompt caching, batches and structured outputs as you scale.
The API is Level 7 of Claude AI Mastery Part 2, starting with session nine. This tutorial gives the essentials; always check the official API documentation for current models, parameters and prices.
How do you get started?
pip install anthropic.import os
import anthropic
client = anthropic.Anthropic(api_key=os.environ["ANTHROPIC_API_KEY"])
message = client.messages.create(
model="<current-model-id>", # pick from the models page in the docs
max_tokens=500,
system="You summarise business documents for Indian SME owners in plain English.",
messages=[{"role": "user", "content": "Summarise this vendor contract in 5 bullet points: ..."}],
)
print(message.content[0].text)The system prompt sets behaviour; the messages hold the conversation. Model IDs change as new versions release, so read the current list in the documentation.
How should you choose a model?
Test the smallest model that meets your quality bar on real examples. Many document and classification pipelines run well on smaller models.
How do tokens and costs work?
You pay per token for input (what you send) and output (what Claude writes), with different rates per model. A token is a piece of a word; English text averages roughly three to four characters per token, and Indian languages often use more tokens per word. Estimate cost by running a sample and reading the usage figures returned with each response.
max_tokens sensibly.How do you get reliable structured output?
For code that reads Claude’s answer, ask for JSON with a defined schema, or define a tool with an input schema and let Claude call it; the API also supports structured output features. Validate the result in code and retry or flag on failure.
prompt = """Extract fields from this invoice. Return only JSON:
{"supplier": str, "gstin": str|null, "invoice_no": str, "date": "YYYY-MM-DD", "total": number}
Invoice text: ..."""
# parse with json.loads(), validate types, and send failures to a review queueNever put your API key in front-end code, a public repository or a shared spreadsheet. If a key leaks, revoke it in the Console immediately and create a new one. Set spending limits in the Console as a safety net.
Careers Ninza is an independent training institute. We are not affiliated with, sponsored by or endorsed by Anthropic, and our certificates are not Anthropic certifications. Claude is a trademark of Anthropic. Product features and plans change often; check claude.com for current details.
What can you build in the first weeks?
Source: Careers Ninza teaching illustration of relative cost; check current prices in the documentation.
What are the limits?
API outputs can be wrong like any model output, so production systems need validation, logging and human review for important decisions. Rate limits apply per account tier. Personal data sent to the API is processed by Anthropic under its terms; minimise it and follow India’s DPDP Act. And APIs change: pin model versions and test before upgrading.
What does a complete small project look like?
A worked example: a Kolkata freight forwarder receives 300 shipping documents a week by email and wants a daily summary of consignments, ports and expected dates. A simple Python project:
This uses only the Messages API, PDF input, structured output and basic Python. It runs on a small server or scheduled job, and with Claude Code helping write it, a non-developer who completed Part 1 can build it in Part 2.
How should errors and retries be handled?
Where do tools, MCP and agents fit?
Once simple calls work, tool use lets Claude call your functions (look up a customer, check stock), MCP lets you connect Claude to systems through a standard protocol, and the Claude Agent SDK provides a framework for agents that plan and act over several steps. Our posts on MCP and the Agent SDK cover these next steps, which make up the later sessions of Part 2.
KEY TAKEAWAYS
Our Claude AI Mastery Part 2 batches run live online, so the same class reaches learners and business owners in metros such as Delhi NCR, Mumbai, Hyderabad, Chennai and Kolkata, growing cities such as Patna, Bhopal, Visakhapatnam, Chandigarh and Ranchi, and smaller industrial towns such as Moradabad, Erode, Bhilwara, Siliguri and Jamshedpur, at the same fee. Each city page explains how local businesses use Claude API, for example in Patna, Moradabad, Jamshedpur.
Learn this live: Claude AI Mastery Part 2
Use the Claude API with tools, structured outputs, retrieval, MCP and agents, and ship a deployed capstone. 30 live hours, ₹29,999, after Part 1.
Build, deploy and maintain software, automations and agents with Claude Code, the Claude API and MCP. Levels 6 and 7, for developers and ambitious non-programmers who have completed Part 1.
Frequently asked questions
Related reading
We teach this, live
Every article here comes from something we teach. Sit in on a free masterclass and judge the mentors yourself.