Use cases · Claude Code

Data analysis with Claude Code: queries, CSVs and a weekly digest

Claude Code isn't only for writing code. Pointed at a database, a warehouse CLI or a folder of CSV exports, it writes the queries and scripts, runs them, checks the results and explains what moved. This page covers how to give it data safely, the rules for CLAUDE.md and how to get answers back as JSON for a dashboard or a weekly report.

What Claude Code does for data analysis

  • Answers questions with SQL against a read-only role or replica, and shows the query with the answer.
  • Cleans and analyses CSV exports with Python, and draws the charts.
  • Writes a weekly metrics digest: what moved, by how much, and the likely reason.
  • Flags anomalies, such as a drop in sign-ups or a spike in errors, before someone has to notice.

Claude Code features for data analysis

FeatureWhat it doesHow to start
Database CLI or MCP serverQueries your database or warehouse; give it a read-only rolepsql, bq or your warehouse's MCP server via claude mcp add
Python in the sessionCleans CSVs, joins exports and draws chartsAsk for an analysis of a file in the working folder
--json-schemaReturns the answer as JSON that matches your schemaclaude -p ... --output-format json --json-schema
Scheduled tasksA weekly digest without opening a session/schedule weekly metrics digest on Monday at 9am

How to set up Claude Code for data analysis

  1. Connect the tools

    Create a read-only database role and give Claude Code that connection, or your warehouse CLI signed in as a read-only user. For one-off analysis, put the CSV exports in the working folder.

    -- Postgres: a read-only role for the agent
    CREATE ROLE analyst LOGIN PASSWORD '<password>';
    GRANT pg_read_all_data TO analyst;
  2. Write the rules in CLAUDE.md

    Claude Code reads CLAUDE.md at the start of every session, claude -p runs included. Put the job's rules there, or in a skill under .claude/skills/ if they're long. See the CLAUDE.md guide.

    ## Data rules
    - Read-only: use the analytics role, never the app's database
      user. No INSERT, UPDATE, DELETE or schema changes.
    - Show the query or script behind every number.
    - Compare with the period before, and say when a change is
      within normal noise.
    - Don't export personal data; aggregate before you write
      anything down.
    - Weekly digest: the five numbers that matter, what moved, and
      one question worth asking next week.
  3. Run it without the interactive interface

    claude -p runs one task and exits, so cron, CI or another tool can start it. Choose what it may do without asking with --allowedTools or --permission-mode; --output-format json returns the result with its cost.

    # Last week's numbers as JSON for a dashboard or a post
    claude -p "Compute last week's sign-ups, activation and revenue vs the week before" \
      --output-format json --json-schema "$(cat metrics.schema.json)"
  4. Put it on a schedule

    Make it a cloud scheduled task with /schedule (Pro, Max, Ultra, Team and Enterprise), a desktop scheduled task, or /loop while a session stays open. See how to make Claude Code proactive.

Source: Claude Code documentation

Limits and safety

  • Give it a read-only role. An agent with write access to the database can change it.
  • It can misread what a column means. Write a short data dictionary and check its first answers.
  • Large results cost context and usage. Query and aggregate before reading rows.
  • Since Claude Code v2.1.302, claude -p starts in Manual mode: an action that needs approval is denied unless a rule allows it. Allow only the tools the job needs.

Claude Code or Codex for data analysis?

The rules carry over. Write them once in AGENTS.md and put @AGENTS.md in CLAUDE.md, and both agents read the same file.

Step by step for the other agent: data analysis with Codex.

Claude CodeCodex
Rules fileCLAUDE.mdAGENTS.md
Connect toolsclaude mcp addcodex mcp add or config.toml
Run without youclaude -pcodex exec
Schedule/schedule, desktop scheduled tasks, /loopScheduled tasks in the ChatGPT app, cron
Built in for data analysis--json-schema structured output--output-schema structured output
With Crewmark

Make data analysis someone's job

Commands run when someone remembers to run them. Crewmark turns your own Claude Code into an AI employee with a role, a goal in numbers, working hours and a heartbeat, so the work happens on schedule and shows up on a board.

  1. Start from the role

    The AI employee roles, with these rules in its brief.

  2. Put the work on its board

    Add the job as a recurring issue. The employee wakes on its heartbeat, does it and reports in a comment.

  3. Related

    The daily SEO report is a worked example of a recurring analytics job.

FAQ

Can Claude Code do data analysis?

Yes. It writes and runs SQL and Python, reads CSVs, draws charts and explains the results. Give it a read-only role and ask it to show the query behind every number.

How do I get structured output from Claude Code?

Run claude -p with --output-format json and --json-schema; the answer comes back in the structured_output field.

Is it safe to connect Claude Code to a production database?

Connect it to a read replica or with a read-only role, never the application's own user, and keep personal data aggregated.

Checked against Claude Code's and OpenAI's Codex documentation on November 13, 2026.

Hand data analysis to an AI employee on Claude Code.

Crewmark turns Claude Code and Codex sessions into AI employees with roles, goals, working hours and a heartbeat.