Jieun 15 min read Updated
Systems

Validating LLM-Generated Search Queries Against 45,000 Production Records

How I replaced a temporary rule for building search queries with a search phrase generated by the LLM and validated in code, then used eight weeks of production search history to check whether the collected documents moved closer to each question.

On this page

Introduction

Wiblog, which I worked on, is a blogging SaaS for business owners. A business registers keywords for its field, and AI writes posts in that field every day and publishes them to the blog through an automated pipeline. Before writing a post, the pipeline searches Google for reference material and builds the body from what it finds. So the query used for that search decides what the post is based on.

But the query often did not match the post. Take a real-estate blog as an example. When the business registers 전세 재계약 계약서 (lease renewal contract) as a keyword, a post such as “Do I need a new fixed-date stamp after signing a lease renewal contract?” is created. What this post needs is material about the fixed-date stamp. Yet the query used to find material was 전세 재계약 계약서 후. The word attached to the query was 후 (“after”), not “fixed-date stamp.”

This post is a record of changing how that query is built. Two things improved after the change.

  • The query now carries the post’s question. The query for the post above became 전세 재계약 계약서 확정일자 다시 (“fixed-date stamp again”). What the search is looking for is visible in the query.
  • The material found moved closer to that question. In eight weeks of production records, the words of the phrase attached to the query appeared in the titles of the documents found at a rate of 29.5% with the new method, against 15.0% for topics searched with the legacy method in the same period, about twice as often. The number of documents returned per search did not drop.

Applying both methods to the same titles changes the queries as follows. These are three topics generated in production, and the legacy queries are the result of re-running the final version of the legacy method on the same titles.

Post title (English gloss)Query from the legacy methodQuery from the new method
Who pays the brokerage fee when writing a lease renewal contract?전세 재계약 계약서 작성 (writing)전세 재계약 계약서 복비 부담 (who bears the brokerage fee)
How do I reflect the 5% increase cap in a lease renewal contract?전세 재계약 계약서 5전세 재계약 계약서 5% 인상 한도 (5% increase cap)
Do I need a new fixed-date stamp after signing a lease renewal contract?전세 재계약 계약서 후 (after)전세 재계약 계약서 확정일자 다시 (fixed-date stamp again)

The service operates in Korean. Queries, titles, and extracted words below are the original Korean strings from the records, with English glosses added for this post. No experiment was re-run in English.

The rest of the post uses three terms.

  • Seed keyword: the keyword a user registers. In the example above it is 전세 재계약 계약서.
  • Topic: a post subject that the LLM (Grok) proposes from one seed keyword. Three come out at a time, and the three rows of the table above are an example. I call the three topics from one seed keyword sibling topics.
  • Query: what each topic uses to find material on Google. It is built as seed keyword + something attached per topic.

At first, for schedule reasons, I implemented the per-topic part with a rule that picks it from the start of the title. I call this the legacy method. Later I changed it so that the LLM that writes the title also produces a phrase to attach to the search. I call this the new method.

1. The Legacy Method Attached a Word From the Start of the Title

The legacy method split the title into words, read them from the front, and attached the first word that passed the exclusion checks to the seed keyword as the extra word. The exclusion checks removed words that overlap the seed keyword and filler words that appear in any title, such as 총정리 (roundup) and 가이드 (guide). It was quick to build and good enough to make each topic’s query different. But it only filtered whether a word was usable in a search. It never compared which word was the point of the question.

How often this limit actually showed up became clear only after I started logging queries. Before that, only the final sources were stored on a topic, so there was no way to see which query had produced them. So I first added a search history table that records, for every search request, the seed keyword, the title, the actual query, and the version of the query-building rule. The version is bumped whenever the rule changes, so the same table can be split by method and compared. Every measurement in this post comes from this search history.

On the day the search history was added (October 2, 2026), the sibling topics for the seed keyword 홋카이도 온천 여행 (Hokkaido hot spring trip) were recorded as follows.

Title at search time (English gloss)Selected extra wordActual query
How do Hokkaido hot spring trip package prices vary by region and itinerary?패키지 (package)홋카이도 온천 여행 패키지
What advantages come up most in real reviews of Hokkaido hot spring trip packages?패키지 (package)홋카이도 온천 여행 패키지
What are the cancellation fees and refund rules when booking a Hokkaido hot spring trip package?패키지 (package)홋카이도 온천 여행 패키지

Topics about price, reviews, and cancellation fees all used the same query. 패키지 was not important to any of them. It was selected because it came right after the seed keyword in all three titles.

Revising the rule only produced different words

I first tried to fix it within the rule. Skipping extra words already used by a sibling topic made the three queries different.

A: 홋카이도 온천 여행 패키지 (package)
B: 홋카이도 온천 여행 실제 (actual) ← it was looking for "reviews"
C: 홋카이도 온천 여행 예약 (booking) ← it was looking for "cancellation fees"

Queries being different from each other and each query matching its question are two separate conditions. This rule is the final version of the legacy method. In the four days before the new method was applied (October 2 to 6), 1,378 topics were searched with this version, and four of the six most frequently selected extra words were connectives: 후 (after, 31), 중 (during, 21), 시 (when, 20), and 어떤 (which, 18). The 시 in “when writing the contract” says nothing about what needs to be researched.

Growing the list of excluded words does not help either. Add 후 and 작성 (writing) takes its place. What was needed was a judgment that reads the meaning of the sentence, and the pipeline already had a component that knew that meaning while writing the title: the LLM that generates it.

2. In the New Method, the LLM That Writes the Title Also Writes the Search Phrase

I added two fields to the JSON output of the LLM call that generates topics. There is no separate call, so the structure of one call per seed keyword and its cost stay the same.

{
"target_keyword": "전세 재계약 계약서",
"title": "전세 재계약 계약서 작성 시 복비는 누가 내야 하나요",
"core_question": "전세 재계약 계약서 작성 시 복비는 누가 내야 하나요?",
"query_focus": "복비 부담"
}
  • target_keyword is the seed keyword.
  • query_focus is the short phrase attached after the seed keyword. I call it the search phrase in this post. It takes the place of the legacy method’s extra word.
  • core_question is the one-sentence question the post must answer. It is not used in the query. It records which question the search phrase came from.

The prompt states that the search phrase should be one to three words and must not invent a year, region, or condition that is not in the title. Sibling topics are allowed to share words. “Tax credit conditions” and “tax credit limit” share a word but need different answers.

LLM output becomes a query only if it passes validation

Reading the meaning was left to the LLM, but the search has to run even when a value is missing or a whole sentence comes back. So code validates the format of the search phrase. There are two outcomes.

  • Semantic query: a query built as seed keyword + search phrase after passing validation.
  • Fallback: the phrase failed validation and the query was built by the legacy method.

The diagram below shows the path from the LLM response to a confirmed query. semantic-v1 and v3 in the diagram are the rule versions written to the search history, for the new method and the final version of the legacy method.

Flow in which a search phrase from the Grok response becomes a semantic query when it passes validation, or falls back to the legacy method when it fails, before being stored in a snapshot and the search history.
Flow in which a search phrase from the Grok response becomes a semantic query when it passes validation, or falls back to the legacy method when it fails, before being stored in a snapshot and the search history.

This is the validation code.

export function semanticQueryFallbackReason(coreQuestion?: string, queryFocus?: string) {
if (!coreQuestion) return 'missing_core_question';
if (!queryFocus) return 'missing_query_focus';
if (coreQuestion.length > 500) return 'core_question_too_long';
if (queryFocus.length > 80 || queryFocus.split(/\s+/).length > 3) return 'query_focus_too_long';
if (!/[\p{L}\p{N}]/u.test(queryFocus) || FUNCTION_ONLY.has(queryFocus))
return 'uninformative_query_focus';
return undefined;
}

FUNCTION_ONLY is a list of function words, such as particles and question words, that carry no meaning on their own. One more check happens outside this function: a search phrase identical to a sibling topic’s phrase is also rejected. I settled three things in the design.

  • No second call. Only the rejected topic falls back to the legacy method.
  • No truncation. Using only the front of a long phrase would send out a query whose meaning has changed without anyone noticing.
  • Rejected values are stored too. The rejected search phrase and its reason go into the search history. The comparison in section 3 and the improvement in section 4 both come from this record.

The query survives title rewrites

A failed search is retried later. In the meantime the outline may be generated, and the topic’s title is rewritten to fit the content. Rebuilding the query from the rewritten title at retry time would send the same topic out with a different query on each attempt. So the generation-time title, question, search phrase, and query are stored as a snapshot, and the search reads that snapshot.

Production data confirmed this was needed. Of 30,171 topics that had finished outline generation, 29,229 (96.9%) had a current title different from the generation-time title. The snapshot is also what makes it possible to re-run the legacy method on the same titles in section 3.

3. Eight Weeks of Search History Show Collected Documents Closer to the Question

The measurement uses the search history accumulated over eight weeks, from October 6 to November 30, 2026. In that period 45,643 topics ran a search, and their queries were built through these paths.

PathTopicsShare
Semantic query41,72091.4%
Fallback because the search phrase exceeded three words3,9178.6%
Fallback because the core question was missing60.01%

Re-running the legacy method on the same titles

First I checked which extra word the legacy method would have picked. Because the snapshot keeps the generation-time title, I re-ran the legacy method unchanged on the titles of topics whose snapshot holds a semantic query. Since the titles are identical, any difference comes only from how the query is built.

In 14,881 of the 41,518 topics where the legacy method picked an extra word (35.8%), that word was not in the LLM’s search phrase at all. These are cases where the two methods were looking for different things. In the remaining 64.2% the extra word was inside the search phrase, but the phrase averaged 2.4 words, so other words were usually added (“conditions” became “tax credit conditions”).

Here are the three topics from the introduction, each with the title of one document collected by the new method’s query.

Generation-time title (English gloss)Legacy extra wordNew search phraseExample title of a document collected by the new method
Who pays the brokerage fee when writing a lease renewal contract?작성 (writing)복비 부담 (who bears the brokerage fee)A question about brokerage fees when renewing a lease
How do I reflect the 5% increase cap in a lease renewal contract?55% 인상 한도 (5% increase cap)Can a lease renewal be raised by only 5%?
Do I need a new fixed-date stamp after signing a lease renewal contract?후 (after)확정일자 다시 (fixed-date stamp again)Do I have to file for a fixed-date stamp again when renewing a lease?

Comparing two groups within the same period

A before-and-after comparison mixes in whatever else changed in between. Instead I compared two groups that went through the same pipeline during the same eight weeks.

  • Semantic-query group: topics searched with a query from the new method
  • Fallback group: topics whose search phrase exceeded three words and were searched with a query from the legacy method

The fallback group also has the LLM’s search phrase stored. So both groups can be checked by the same criterion: did what the LLM was looking for show up in the collected documents? I split the search phrase into words, removed words that overlap the seed keyword, and checked whether the remaining words appear in the collected documents.

The comparison covers topics whose collected documents are stored on the topic, so their content can be checked. For the semantic-query group that is a random sample of 3,410 topics, and for the fallback group it is all 1,539 such topics.

MetricSemantic-query groupFallback group
Share of collected document titles containing the search phrase words (per-topic mean)29.5%15.0%
Share of full-text documents whose body contains the search phrase words61.6%50.2%
Share of full-text documents containing all of the search phrase words30.1%13.4%

All three metrics were higher for the semantic-query group, and the first is about twice as high. The difference computed from unrounded values is 14.6 percentage points, and a 95% interval from recomputing over resampled topics is 12.9 to 16.2 points.

I also checked in the search history whether longer queries reduced the results. Candidate documents returned per search averaged 7.6 for the semantic-query group and 7.5 for the fallback group, and the share of failed search requests was 7.2% and 7.3%.

This comparison has limits.

  • Assignment was not random. The fallback group consists of topics whose search phrase has four or more words, so the questions themselves may be more complex, and having more words puts them at a disadvantage on the “all words” metric.
  • The semantic-query group searched with those very words, so the words are more likely to appear in titles. This metric shows how far the intended search is reflected in the results. It does not show that a document answers the question.

There is also a number I do not count as a result. The overlap of collected document URLs between sibling topics (Jaccard similarity) dropped from 0.072 before the new method to 0.011 for the semantic-query group after it. But the fallback group after the change was also at 0.015. Two groups that build queries differently dropped together, so this decrease cannot be credited to the new method. Looking only at before and after, without a group to compare against, I would have reported it wrongly.

4. The 8.6% That Fell Back Is the Next Improvement

The fallback group consists of topics whose search phrase exceeded the three-word limit. I set that limit conservatively at first because a longer query might return fewer results. The rejected search phrase and the query that actually went out are both in the search history, so they can be compared.

Seed keyword (English gloss)Rejected search phraseQuery that actually went out
협력업체 공정 회의 운영 (running subcontractor schedule meetings)설계 변경 일정 조정 (adjusting the schedule for design changes)협력업체 공정 회의 운영 설계 (design)
테슬라 플라잉카 (Tesla flying car)다른 AI 비행체 비교 (comparison with other AI aircraft)테슬라 플라잉카 다른 (other)
공인중개사 개업 방법 (how to open a real-estate agency)첫 고객 유치 방법 (how to win the first clients)공인중개사 개업 방법 후 (after)

The rejected search phrases carry the question better than the extra words that went out instead. This is also why the fallback group scores lower in the table in section 3. Of the 3,917 rejected topics, 3,541 had four-word phrases, so allowing four words would be expected to reduce fallbacks from 8.6% to about 0.8%.

I learned two things from this result.

  • The instruction in the prompt and the rejection threshold in code should be different values. “Write three words or fewer” is a direction given to the model. “Do not use it if it has four words” is a decision to discard a result. The rejection threshold should be looser than the instruction.
  • A rejection threshold should be set by comparing against what goes out instead. The criterion is not “is this value ideal?” but “is this value worse than its replacement?” Here, a four-word search phrase was better than a single extra word from the legacy method.

The change to allow four words has not been applied yet. When it is, bumping the rule version will let it be compared again with the same metrics as in section 3.

Conclusion

I replaced the legacy method, which went in first for schedule reasons, with a new method in which the LLM that writes the title also writes the search phrase. In eight weeks of search history, the collected documents moved closer to the question. The search phrase words appeared in collected document titles at 29.5% for the semantic-query group and 15.0% for the fallback group, and the number of documents returned did not drop.

I can state this result because records were kept along with the change. Queries were stored with their rule version, and both the phrases that were used and the ones that were rejected were kept. The same search history also showed the next improvement, the 8.6% that fell back.

Some things remain unverified. I did not measure whether the collected documents actually answer the question, or whether the quality of the final posts improved.

Assigning documents to sibling topics without duplicates is covered separately in How Sibling Topics Share Search Results Without Receiving the Same Document.

Comments