π§ Advanced: Agents & API Integration
Build autonomous multi-step research pipelines, call the Claude API directly, and create custom AI tools tailored to your specific research needs.
In this module
What are agents?
Claude Code is already an agentic system β it can plan and execute multi-step tasks. This module shows you how to go further: build pipelines that run autonomously, and call Claude from your own code.
Calling the Claude API directly
You can call Claude from Python or R scripts for batch processing tasks β analysing hundreds of survey responses, coding qualitative data, or classifying documents at scale.
# Install: pip install anthropic
import anthropic
client = anthropic.Anthropic() # Uses ANTHROPIC_API_KEY env variable
# Simple call
message = client.messages.create(
model="claude-sonnet-4-6",
max_tokens=1024,
messages=[
{"role": "user",
"content": "Summarise this survey response in 2 sentences: [response]"}
]
)
print(message.content[0].text)
# Batch process survey responses
import pandas as pd
df = pd.read_csv("data/qualitative_responses.csv")
df["summary"] = df["response_text"].apply(lambda text:
client.messages.create(
model="claude-haiku-4-5-20251001", # cheaper for batch
max_tokens=200,
messages=[{"role": "user", "content": f"Summarise: {text}"}]
).content[0].text
)
df.to_csv("data/responses_with_summaries.csv", index=False)
# Install: install.packages("httr2")
library(httr2)
library(jsonlite)
call_claude <- function(prompt, model = "claude-haiku-4-5-20251001") {
api_key <- Sys.getenv("ANTHROPIC_API_KEY")
request("https://api.anthropic.com/v1/messages") |>
req_headers(
"x-api-key" = api_key,
"anthropic-version" = "2023-06-01",
"content-type" = "application/json"
) |>
req_body_json(list(
model = model,
max_tokens = 1024,
messages = list(list(role = "user", content = prompt))
)) |>
req_perform() |>
resp_body_json() |>
pluck("content", 1, "text")
}
# Batch code qualitative responses
df <- readr::read_csv("data/interviews.csv")
df$theme <- purrr::map_chr(df$response, ~ call_claude(
glue::glue("Classify this interview response into one of: [Economic, Social, Political, Other]. Response: {.x}")
))
readr::write_csv(df, "data/interviews_coded.csv")
Building research agents
Literature review agent
> Build an agent that does an automated literature review.
Given a research question, it should:
1. Search for relevant papers (using Brave Search MCP or a provided list)
2. For each paper, extract: title, authors, year, RQ, method, findings
3. Identify themes and contradictions across papers
4. Draft a structured literature review with citations
5. Flag any papers it couldn't access
Research question: "What is the effect of mobile money on household savings in Africa?"
Search for the 20 most relevant papers from 2015β2025.
Data quality agent
> Create a reusable data quality agent that, given any CSV file:
1. Profiles the data (types, N, missing %, unique values)
2. Detects outliers using IQR method for numeric variables
3. Identifies potential duplicate records
4. Checks for common data entry errors (e.g., "999" or "-99" as missing codes)
5. Generates a full HTML quality report
6. Recommends cleaning steps
Save as agents/data_quality_agent.py and make it runnable from command line:
python agents/data_quality_agent.py data/raw/survey.csv
Sub-agents in Claude Code
Claude Code can spin up sub-agents to work on tasks in parallel β useful for large research projects:
> I need to process 50 interview transcripts and extract key themes.
Use sub-agents to process them in parallel:
- Spawn 5 agents, each handling 10 transcripts
- Each agent extracts: main themes, key quotes, sentiment
- Combine results into a single summary file
Save final output to output/interview_analysis.json
Three-agent verification (research applications)
The most powerful multi-agent pattern for academic research isn't parallelisation β it's independent verification. Two agents read the same source independently, then a third cross-checks both:
> Read the 10 papers in papers/to-read/ using a three-agent approach.
For each paper:
- Agent A: read the PDF and extract key claims, methods, and findings
- Agent B: read the same PDF independently (no access to Agent A's output)
- Agent C: cross-compare A and B against the original PDF.
For any discrepancy, flag it. For anything unverifiable, write FLAG FOR AUTHOR.
Output per paper:
- Consolidated annotation (~150 words)
- Discrepancy log (Agent A vs B differences)
- List of FLAGS FOR AUTHOR
Save to output/verified_annotations.docx
Adversarial multi-agent review before shipping
When building tools or products, use parallel agents with different "hats" to stress-test your work before sharing it:
> Review this analysis (output/main_results.tex + analysis/main.do)
using five parallel review agents:
1. Statistician β correct interpretation of coefficients, SE, p-values
2. Data engineer β data construction errors, merge issues, missing-value handling
3. Replication specialist β can someone replicate this from the code + data?
4. Referee β what would a skeptical journal reviewer find to object to?
5. Author β does the text accurately describe what the code actually does?
Each agent: flag issues as Critical / Major / Minor with suggested fix.
Cost management at scale
| Task type | Recommended model | Why |
|---|---|---|
| Batch text classification | Claude Haiku | Cheapest, fast, good at classification |
| Complex code generation | Claude Sonnet | Best balance of quality and cost |
| Research strategy, complex analysis | Claude Opus | Highest capability for hard tasks |
| Simple Q&A, formatting | Claude Haiku | No need for expensive model |
Module 12 Exercise: Your first API call
- Set your API key as an environment variable:
export ANTHROPIC_API_KEY="sk-ant-..." - Write a Python or R script (ask Claude to help) that calls Claude to classify 10 rows of text from one of your datasets
- Try it with both Haiku and Sonnet and compare quality and cost
- As a bonus: build the data quality agent described above and run it on one of your CSV files