Module 12 Β· Advanced Β· ~60 min

🧠 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.

What are agents?

πŸ“–
Plain English: what is an AI agent? A regular AI interaction is one question, one answer. An agent takes a goal and works autonomously through multiple steps to achieve it β€” reading files, running code, making decisions, correcting errors β€” without you needing to direct every step. Think of the difference between asking someone "what's 2+2?" (one answer) versus asking them to "prepare a complete tax return for me" (many steps, decisions, tasks).

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
πŸ’‘
Why three agents catch more errors Two independent reads of the same PDF catch OCR errors, decimal transcription errors, and misattributed quotes that a single agent would miss. Agent C acts as the referee β€” it resolves genuine discrepancies by going back to the PDF source.

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 typeRecommended modelWhy
Batch text classificationClaude HaikuCheapest, fast, good at classification
Complex code generationClaude SonnetBest balance of quality and cost
Research strategy, complex analysisClaude OpusHighest capability for hard tasks
Simple Q&A, formattingClaude HaikuNo need for expensive model
πŸ’°
Cost-saving rule of thumb Use the cheapest model that gives acceptable quality for your task. For batch processing 1,000 documents, the difference between Haiku and Opus can be $5 vs. $300. Test on 10 examples first with the expensive model, then switch to Haiku for the full run if results are good enough.
🎯

Module 12 Exercise: Your first API call

  1. Set your API key as an environment variable: export ANTHROPIC_API_KEY="sk-ant-..."
  2. Write a Python or R script (ask Claude to help) that calls Claude to classify 10 rows of text from one of your datasets
  3. Try it with both Haiku and Sonnet and compare quality and cost
  4. As a bonus: build the data quality agent described above and run it on one of your CSV files
Python or RClaude API~45 min