Sub topic 10
Scripting Repeated Runs and Scoring
Assume you can read basic Python and JSON, and you already know how to run a prompt manually or through an assistant API. This chapter uses saved answer records rather than calling a live service. That keeps the scoring repeatable and avoids mixing API errors with measurement results.
Separate prompts, runs, and scores
A prompt is the question you send to an assistant. A run is one execution of that prompt under a specific model, mode, date, and settings. The same prompt may produce different answers on different runs.
Store the answer text and its conditions together. A minimal record looks like this:
{
"prompt_id": "cmp-01",
"run": 2,
"assistant": "Example Assistant",
"mode": "web",
"date": "2026-11-29",
"settings": {"temperature": 0.2},
"answer": "Acme Analytics is a good choice for small teams. Example BI has stronger reporting."
}Use a stable prompt_id so repeated runs can be grouped later. Do not replace an old answer when you run the prompt again. Keep every record so that you can audit a score.
flowchart TD
A[Prompt set] --> B[Repeated runs]
B --> C[Saved answer records]
C --> D[Per-answer scoring]
D --> E[Aggregate rates]
E --> F[Comparison and trend report]caption: From prompts to aggregate visibility metrics alt: A flowchart showing prompts becoming repeated runs, saved records, scores, aggregate rates, and reports.
Detect mentions without double counting
A simple first pass can find brand and competitor names. Whole-word matching prevents a short name from matching part of a longer word. Case-insensitive matching treats “Acme” and “acme” alike.
Aliases need special handling. If “Acme” is an alias for “Acme Analytics,” count the answer as mentioning the brand once, not twice. The function below checks aliases in order and returns canonical names.
import re
from collections import Counter, defaultdict
BRANDS = {
"Acme Analytics": ["Acme Analytics", "Acme"],
"Example BI": ["Example BI"],
"Sample Metrics": ["Sample Metrics"],
}
def find_mentions(answer: str, brand_aliases: dict[str, list[str]]) -> list[str]:
"""Return canonical brands mentioned at least once."""
found = []
for canonical, aliases in brand_aliases.items():
for alias in aliases:
pattern = r"\b" + re.escape(alias) + r"\b"
if re.search(pattern, answer, flags=re.IGNORECASE):
found.append(canonical)
break
return found
def score_answer(record: dict) -> dict:
mentions = find_mentions(record["answer"], BRANDS)
brand = "Acme Analytics"
position = None
if brand in mentions:
positions = [
match.start()
for alias in BRANDS[brand]
for match in re.finditer(
r"\b" + re.escape(alias) + r"\b",
record["answer"],
flags=re.IGNORECASE,
)
]
position = min(positions) + 1
return {
**record,
"mentions": mentions,
"brand_mentioned": brand in mentions,
"brand_position_character": position,
"competitor_count": sum(name != brand for name in mentions),
}
def mention_rates(scored_records: list[dict]) -> dict[str, float]:
totals = Counter()
hits = Counter()
for record in scored_records:
prompt_id = record["prompt_id"]
totals[prompt_id] += 1
hits[prompt_id] += int(record["brand_mentioned"])
return {
prompt_id: hits[prompt_id] / totals[prompt_id]
for prompt_id in totals
}
records = [
{
"prompt_id": "disc-01",
"run": 1,
"answer": "Acme Analytics is useful for a small online shop."
},
{
"prompt_id": "disc-01",
"run": 2,
"answer": "Consider Example BI for simple reporting."
},
{
"prompt_id": "disc-01",
"run": 3,
"answer": "Acme offers useful funnel reports for small shops."
},
]
scored = [score_answer(record) for record in records]
print(mention_rates(scored))
# {'disc-01': 0.6666666666666666}The position value here is the character position of the first mention. For reporting, you may prefer a simpler category such as first, middle, or absent. Character position is useful for automation, but it is not the same as editorial prominence.
Add human review for meaning
Name matching cannot determine whether an assistant recommends a brand, criticizes it, or states an incorrect fact. Add review fields after the automated pass:
| Field | Example values |
|---|---|
| recommendation | positive, neutral, negative, unclear |
| citation_present | yes, no |
| factual_accuracy | correct, partly correct, incorrect, unchecked |
| reviewer_note | “Pricing claim needs verification” |
Keep automated and human scores separate. This lets you improve the rules without losing the original answer.
> callout note > Treat automated scores as measurements of text patterns, not judgments of meaning. Review a sample of answers manually, especially when the brand name is ambiguous or appears in a negative statement.
Calculate share of voice
Share of voice measures how often the target appears relative to the tracked brands. For each answer, count each canonical brand at most once. Across a prompt set, calculate:
brand mentions / all tracked-brand mentions
Report the denominator. A result based on 12 total brand mentions is less stable than one based on 200.
Run the same script after adding new answers. Compare results only when the prompt set, assistant, mode, and scoring rules are comparable. If any of those change, record a new test version.
Exercise
Three runs mention Acme Analytics twice, and four runs mention it once. What is the mention rate?
Show answer
The rate is 3 divided by 4, or 75%, because the metric counts runs with at least one mention.