Automated Keyword Research and AI Clustering, End to End

By Irina Saprykina, working in SEO and digital marketing since 2006.

Manual keyword research and clustering could swallow a whole day. In this article I'll show how I automate keyword research almost end to end – using Claude Code to write the scripts, a keyword-research API for the data, Python to tie it together, and my own rules for how the grouping should actually work. One run costs from $0.09 in API credits and about 15 minutes.

Below you'll find the whole keyword research workflow in detail – essentially how to automate keyword research yourself – plus a step-by-step setup plan you can follow even if you've never written a line of Python.

Why Automate It at All

Keyword research isn't hard so much as it is repetitive and rule-bound. I have a fairly fixed way of deciding what belongs in a group, what's a separate intent, and what to throw out. That's exactly the kind of judgement you can write down once and reuse, instead of re-explaining it to a person (or to yourself) every time. The goal was never to remove my judgement from the loop, only to stop spending it on the mechanical 80%.

How the Core Pipeline Works

The main flow is five steps, and only the first touches a paid API:

  1. I give it seeds and a region. Seed keywords and a location, and optionally negative keywords and other filters.
  2. The Python script pulls related keywords. Up to 1,000 of them from DataForSEO, each with search volume, competition and a 12-month trend.
  3. The script drafts the clusters. A first-pass grouping (no API cost), saved as a JSON file plus a prompt for Claude to refine the groups according to my rules.
  4. Claude refines the groups. It asks clarifying questions if anything is ambiguous, then returns a cleaned-up JSON, which goes back into the Python script.
  5. I get grouped keywords in Google Sheets. Organised by topic, with volumes, competition and the past 12 months of trend for each keyword.

The split matters: Python does the fetching, deduping and the boring first draft; Claude does the semantic judgement; Python does the writing-up. Each tool does the part it's actually good at, and the expensive API call happens exactly once per run.

The five-step keyword pipeline and who does each step You set the inputs; Python pulls related keywords and drafts rough clusters; Claude refines the groups by your rules; Python writes the result to Google Sheets. You Python Claude 1 You set the inputs Seed keywords, region, optional negative keywords and filters 2 Python pulls related keywords Up to 1,000, each with volume, competition and a 12-month trend 3 Python drafts rough clusters A first-pass grouping in code – no API cost 4 Claude refines the groups Applies your rules, asks if anything is ambiguous 5 Written to Google Sheets Grouped by topic, with volumes and trends attached datairi.com
The pipeline in five steps. Python handles the mechanical work (blue) and Claude the semantic judgement (purple); the paid API is called just once, in step 2.

A tip if you're still tuning your grouping rules: have the script cache the pulled keywords to a local file. Testing a different grouping prompt then means re-running only the grouping step, so you're not charged again for the same keywords every time you restart the script.

Keyword Clustering: The Prompt Does the Real Work

Of the five steps, one deserves singling out, because it's where good clusters are won or lost: the rules I hand Claude in step 4. The Python first pass only buckets keywords crudely. The real call – does running shoes for flat feet belong with best running shoes, or is it a separate search intent? – comes down to how precisely I've written the keyword grouping instructions.

Keyword clustering turns one flat list into groups by search intent A flat, mixed keyword list on the left becomes three intent-based clusters on the right after Claude applies your grouping rules. Before – one flat list best running shoes how to clean running shoes running shoes for flat feet top running shoes 2026 clean smelly running shoes best shoes for flat feet best budget running shoes washing running shoes Claude & your rules After – grouped by intent Best-shoe picks best running shoes top running shoes 2026 best budget running shoes Shoes for flat feet running shoes for flat feet best shoes for flat feet Cleaning & care how to clean running shoes clean smelly running shoes washing running shoes datairi.com
The same eight keywords, before and after. Claude splits them by intent using your rules – so "best running shoes" and "running shoes for flat feet" land in separate groups rather than one messy pile.

That prompt is the heart of the whole workflow. It's where my domain judgement lives, written down once: what counts as a separate intent, when a modifier splits a group and when it doesn't, what to discard outright, how large a group is allowed to get. Vague rules give you a tidy-looking sheet that's subtly wrong; specific rules give you groups you can actually build pages around. The API pulls the data, but the prompt decides whether the grouping is any good – so that's where I spend my time.

Two Scripts for Going Deeper

Grouped keywords are a map, not the whole job. Two more scripts help me decide how to treat specific keywords and what the winning pages actually look like.

SERP Overlap Check

I usually take the most popular keywords from the main groups and run this script to compare how much their search results overlap. If two keywords return largely the same pages, Google treats them as one intent and they belong in one group; if the results diverge, they should be split. The overlap check is the tie-breaker for the cases where semantics alone don't settle whether two keywords are really the same search. With the overlap data in hand, you can go back to Claude and ask it to re-sort the keywords, taking the new overlap information into account.

How the SERP overlap check decides to merge or split keywords The check compares the actual result URLs of two keywords regardless of their position. When the same URLs rank for both, they share one intent and are merged; when the URLs differ they are split, even if the same site appears on a different page. Scenario A · the same URLs keep appearing best running shoes top running shoes same URL, any rank 1nike.com/running-shoes 2runnersworld.com/best-shoes 3roadtrailrun.com/top-2026 4brooksrunning.com/reviews 1runnersworld.com/best-shoes 2nike.com/running-shoes 3roadtrailrun.com/top-2026 4hoka.com/top-picks 3 of 4 URLs match → same intent → one group Scenario B · the results barely overlap running shoes how to clean running shoes ≠ same site, different URL 1nike.com/running-shoes 2adidas.com/shoes 3runnersworld.com/best-shoes 4amazon.com/running 1wikihow.com/clean-shoes 2youtube.com/watch 3runnersworld.com/clean-guide 4reddit.com/r/running 0 of 4 URLs match → different intent → split datairi.com
The check compares result URLs, not domains: shared URLs mean one search intent (merge the keywords); differing URLs mean separate intents (split them) – even when the same site shows up on a different page.

Competitor Keyword Analysis

For a few chosen keywords, this script reads the SERP and then, for the top-ranking pages, digs into what the competitors are doing. It returns two things:

The result is a picture of both the demand (what else these pages capture) and the execution (how they actually phrase it on the page).

By the end I have keyword groups organised by topic, enriched with competitors' keywords and the language they use on their winning pages.

Keywords for AI Search: Fan-Out Queries

Everything above optimises for what people type into Google. But AI search doesn't run your query once: when you put a question to an assistant like ChatGPT, Gemini or Perplexity, the model quietly expands it into a set of fan-out queries – related sub-questions it retrieves pages for before writing its answer.

How an AI assistant expands one prompt into hidden fan-out queries A single salary-negotiation prompt is expanded by the AI assistant into a set of related sub-questions it searches for before answering, none of which the user sees. Your prompt how to negotiate a salary AI assistant Hidden fan-out queries it runs salary negotiation tips how to prepare to negotiate researching salary benchmarks negotiation scripts & examples how to make a counter-offer responding to a job offer datairi.com
One prompt, quietly expanded. Ask an assistant how to negotiate a salary and it searches a spread of related sub-questions before answering – a real example from my AI citation guide. Some sit close to the original, some barely touch it ("responding to a job offer").

Those fan-out queries are a keyword layer of their own – not just long-tail keywords – and most don't exist in any classic tool: AirOps found roughly 95% have no search volume at all. Covering this layer is the keyword-research side of generative engine optimization (GEO) – optimising to be surfaced and cited by AI engines, not just ranked by Google. (Why fan-out coverage matters for getting cited is a separate topic, covered in my AI citation ranking factors guide.)

DataForSEO's AI Optimization API surfaces this layer two ways, and I feed both into the same pipeline:

The fan-out layer slots into the main flow: seed prompts → LLM Responses → dedupe → AI Keyword Search Volume to rank → the same clustering step. Classic keyword research tells you what to rank for; this tells you what the AI goes looking for once it's answering.

What It Costs (July 2026 Prices)

Here's the current per-run cost in Live mode, reflecting DataForSEO's July 2026 price update. All of these are pay-as-you-go – you're billed per request, not by subscription.

Step Endpoint Price
Related keywords (up to 1,000, with volume, competition, 12-mo trend) Keywords Data → Google Ads → Keywords For Keywords, Live $0.09 per run
Search-volume enrichment (up to 1,000 keywords) Keywords Data → Google Ads → Search Volume, Live $0.09 per 1,000
SERP overlap check SERP → Google Organic, Live Advanced $0.002 per keyword
Competitor keyword analysis DataForSEO Labs → Ranked Keywords, Live $0.01 per task + $0.0001 per keyword returned
AI fan-out harvest AI Optimization → LLM Responses, Live ~$0.0006 base per prompt + the LLM's own token & web-search charges (a few cents per engine)
AI search-volume for fan-outs (up to 1,000) AI Optimization → AI Keyword Search Volume, Live $0.01 per task + $0.0001 per keyword

So, in practice:

Not bad for what used to take the better part of a day.

From Zero: Build This Yourself with Claude Code

Here's the part that trips people up: you don't need to know how to code. I lean on Claude Code – Anthropic's terminal agent – to actually write the Python. My job is to describe the workflow, hand over my rules, and test what comes back. If you've never opened a terminal, this is roughly an afternoon of setup; after that, changes are just conversation.

The order below is the one I'd follow from a clean machine.

1. Install Python

The scripts run on Python, so install it first from python.org.

Check it worked by opening a terminal (PowerShell on Windows, Terminal on macOS) and running python --version – it should print a 3.x version. If it doesn't, Claude Code can diagnose the PATH problem for you in the next step.

2. Install Claude Code

Claude Code needs a paid Claude plan – Pro or Max (the free Claude.ai tier doesn't include it). Then install it one of two ways:

Then make a new empty folder for the project, open a terminal inside it, and run claude. On first launch it opens your browser to log in with your Claude account – no separate API key to manage, since Claude Code bills against your subscription. From here on, you mostly talk to it: "install the libraries you need," "create a .env file for my secrets," and so on. If Python from step 1 isn't found, tell Claude Code and let it walk you through fixing PATH.

3. Get your DataForSEO credentials

The keyword and AI data come from DataForSEO, which is pay-as-you-go with a $50 minimum top-up (that credit lasts a long time at these prices). Sign up, add funds, and copy your API login and password from the dashboard.

Don't paste those into the script. Ask Claude Code to keep them in a .env file (a plain text file that stays on your machine and out of your code), like:

DATAFORSEO_LOGIN=your_login_here
DATAFORSEO_PASSWORD=your_password_here

Tell Claude Code to read the keys from .env and to add that file to .gitignore so you can never accidentally share it. You don't need a separate Anthropic API key for this – Claude Code already covers the Claude side.

4. Give the scripts access to Google Sheets

This is the fiddliest part, because Google makes a script prove it's allowed to touch your sheets. The mechanism is a service account – a robot Google user with its own email address that you explicitly invite to your spreadsheet. Walk through it once:

  1. Go to the Google Cloud Console and create a new project.
  2. Enable two APIs for it: Google Sheets API and Google Drive API.
  3. Create a service account, then create a key for it and download the JSON file. Save it in your project folder (it's usually named something like credentials.json).
  4. Open that JSON file and copy the client_email – it looks like something@your-project.iam.gserviceaccount.com.
  5. Open the Google Sheet you want the script to write to, hit Share, paste that email, and give it Editor access. This is the step everyone forgets – without it the script gets a "permission denied" even though the code is fine.

Then tell Claude Code you have credentials.json in the folder and want to write results to a specific Google Sheet; it'll wire up the gspread library and the rest. Treat credentials.json like a password – add it to .gitignore too.

5. Let Claude Code build the pipeline

Now describe what you want, in plain language. The prompts that worked for me were specific about both the data and the rules:

Expect to go back and forth. You'll test on a small seed, spot something wrong, and describe the fix – that conversation is the development. Start cheap: a single seed keyword is a few cents, so you can iterate a lot before it adds up.

One optional shortcut worth knowing: DataForSEO publishes an MCP server, which you can connect to Claude Code so it can call the DataForSEO API directly while it builds and debugs, instead of you copy-pasting sample responses. It's not required, but it makes the build loop noticeably faster.

None of this needs prior coding skill – it needs a clear description of what you want and the patience to test. The scripts are the easy part now; the judgement about what belongs in a group is still yours, which is exactly as it should be.

Stuck on any of this? Ask Claude.

I'm not the source of the technical know-how here. The whole setup path above – installing Python, wiring up the Google service account, structuring the scripts – is what Claude walked me through in the first place. So if any step in my write-up feels unclear or intimidating, don't get stuck on my phrasing: ask Claude to explain it in more detail, or just paste this article in and have it guide you through your own setup. :)

Other Keyword APIs in DataForSEO Worth Knowing

My workflow only touches a handful of endpoints, but DataForSEO has more keyword-related APIs you could bolt on. A few I keep coming back to (all pay-as-you-go, most of the DataForSEO Labs ones at roughly $0.01 per task plus $0.0001 per keyword returned):

None of these extra APIs are required for the pipeline above; they're just the pieces I reach for when a project needs more than grouped keywords.

Am I Fully Satisfied?

During the build I went through the full range of opinions – from "this is impossible" to "this is almost perfect." My current version works well, though it isn't flawless. I tested it on different kinds of keywords:

The honest framing is the one I keep landing on with these experiments: automation doesn't replace the judgement, it removes the repetition around it. I still make the calls on the wide, ambiguous groups. I just don't spend my day getting there anymore.

About the Author

Irina Saprykina

I'm Irina Saprykina, and I've worked in SEO and digital marketing since 2006, mostly in-house, promoting international SaaS companies. I'm a mathematician by training, which is probably why I'd rather test and automate a process than take a claim on faith. datairi is where I publish those experiments and the tools that come out of them.

← All posts