You've used Claude or ChatGPT in the browser and thought "I could use this inside my own tools." This guide gets you there. By the end you'll have a working Python script that takes any text document and returns a clean summary using the Claude API. It's about 30 lines of code, and it's the same pattern behind most LLM features you'd actually ship: send text in, get useful text back.
We'll build a document summarizer because it's genuinely useful (meeting notes, long emails, contracts, reports) and because once you've got it working, swapping the prompt turns it into a classifier, a drafter, or an extractor with no new plumbing.
Prerequisites
- Python 3.8 or newer installed (check with
python3 --version) - Basic comfort with a terminal and a text editor
- An Anthropic account with a payment method. New accounts get you started with the Console at console.anthropic.com
- About 20 minutes
Step 1: Get an API key
Sign in to the Anthropic Console at console.anthropic.com. In the left navigation, find API Keys and create a new key. Give it a name that tells you where it's used, something like summarizer-dev, so you can revoke it later without guessing.
The key is shown once. Copy it somewhere safe. Treat it like a password: anyone who has it can run up charges on your account.
Set it as an environment variable so it never ends up in your code:
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
To make that stick between terminal sessions, add the line to your ~/.zshrc or ~/.bashrc. Never paste the key directly into a script, and never commit it to git. This is the single most common mistake we see, and keys scraped from public repos get abused within hours.
Step 2: Install the SDK
Anthropic publishes an official Python package. Install it with pip:
pip install anthropic
If you keep projects tidy with virtual environments (good habit), do that first:
python3 -m venv venv
source venv/bin/activate
pip install anthropic
Step 3: Make your first API call
Create a file called hello_claude.py:
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY from the environment
message = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
messages=[
{"role": "user", "content": "Say hello in one sentence."}
],
)
print(message.content[0].text)
Run it:
python3 hello_claude.py
If you see a greeting printed, your key works and you've made your first API call. Three things worth knowing about what just happened:
model="claude-sonnet-5"picks which Claude model answers. Sonnet is the mid-tier model: strong quality, moderate cost, a good default for features like this.max_tokenscaps how long the response can be. Tokens are chunks of text, roughly three-quarters of a word each on average.messagesis a list because conversations can have history. For a one-shot task like summarizing, one user message is all you need.
Step 4: Build the summarizer
Now the real thing. Create summarize.py:
import sys
import anthropic
client = anthropic.Anthropic()
def summarize(text: str) -> str:
message = client.messages.create(
model="claude-sonnet-5",
max_tokens=1024,
system=(
"You summarize business documents. Return: a one-sentence "
"overview, then 3-5 bullet points covering key facts, "
"decisions, and action items. Be specific. No preamble."
),
messages=[
{"role": "user", "content": text}
],
)
return message.content[0].text
if __name__ == "__main__":
if len(sys.argv) < 2:
print("Usage: python3 summarize.py <file.txt>")
sys.exit(1)
with open(sys.argv[1], "r") as f:
document = f.read()
print(summarize(document))
The new piece here is the system parameter. It's standing instructions that shape every response: what role to play, what format to return. Putting the instructions in system and the document in messages keeps the two cleanly separated, which matters more as prompts grow.
Step 5: Run it on a real document
Grab any text file: exported meeting notes, a long email saved as .txt, a project brief. Then:
python3 summarize.py meeting_notes.txt
You should get back a tight overview and a handful of bullets. Try it on a few different documents. If the output format drifts from what you want, edit the system prompt to be more specific. "3-5 bullet points" beats "a few points." Prompts are instructions to a very literal reader; precision pays.
Verify it and understand the cost
Two checks before you trust this with real work.
Accuracy check: summarize a document you know well and read the output critically. Did it capture the decisions? Did it invent anything? Summarization is one of the safer LLM tasks because the source text is right there, but you should still spot-check any output before it goes to a client.
Cost check: API pricing is per token, in and out. The response object tells you exactly what you used. Add this to see it:
print(message.usage.input_tokens, message.usage.output_tokens)
For scale: a five-page document plus its summary is a few thousand tokens total, which works out to a fraction of a cent per summary at Sonnet's rates. You could summarize every document your business produces in a month and the bill would likely be under a few dollars. Cost becomes a real design concern at high volume or with very large documents, but for a feature like this, it's negligible. You can set monthly spend limits in the Console if you want a hard ceiling.
Where to go from here
You now have the core loop: text in, instructions in the system prompt, text out. Everything else is variations. Change the system prompt and this same script classifies support emails by urgency, extracts names and dollar amounts into structured fields, or drafts replies. Wire it to a folder watcher and it processes documents as they arrive. The plumbing you built today doesn't change.
If you've got a workflow in mind and want help getting it from script to something your team actually uses day to day, that's the kind of build we do.
Stuck on this, or want it done for you? That's the job.
Email us →