{"slug": "rewrite-weak-prompts-with-zero-shot-few-shot-and-chain-of-thought", "title": "Rewrite Weak Prompts with Zero-Shot, Few-Shot, and Chain-of-Thought", "summary": "Priya Nair published a tutorial showing developers how to rewrite weak prompts into stable classifiers using zero-shot, few-shot, and chain-of-thought patterns, with runnable Python code verified against the Anthropic SDK 1.4.0 and the claude-opus-5 model. The guide demonstrates turning a vague support-ticket prompt into parseable category labels by adding explicit rules, examples, and visible reasoning, addressing common prompt engineering failures.", "body_md": "# Rewrite Weak Prompts with Zero-Shot, Few-Shot, and Chain-of-Thought\n\nTurn one vague prompt into a stable, parseable classifier using three prompting patterns, in runnable Python.\n\n[Priya Nair](https://sourcefeed.dev/u/priya_nair)\n\n## What you'll build\n\nA single Python script that takes one vague, unreliable prompt and rewrites it three ways: zero-shot with explicit instructions, few-shot with examples, and chain-of-thought with visible reasoning. You'll run all four against the same ambiguous support ticket and watch the output go from chatty prose to a stable, parseable label.\n\n## Prerequisites\n\n- [Python](https://www.python.org/) 3.10 or newer (`python3 --version` ). The Anthropic SDK requires 3.10+.\n- An Anthropic API key from the [Claude Console](https://platform.claude.com/settings/keys) , with a few cents of credit on the account.\n- Verified against the [`anthropic`](https://pypi.org/project/anthropic/) Python SDK 1.4.0 and model`claude-opus-5` on macOS; Linux is identical, and on Windows use`.venv\\Scripts\\activate` instead of`source` .\n\n## 1. Set up the project\n\n```\nmkdir prompt-patterns && cd prompt-patterns\npython3 -m venv .venv && source .venv/bin/activate\npip install anthropic\nexport ANTHROPIC_API_KEY=\"your-api-key-here\"\n```\n\nThe SDK reads `ANTHROPIC_API_KEY` from the environment, so the code never touches the key directly.\n\n## 2. Start with the weak prompt\n\nCreate `prompts.py`. The task: route support tickets into one of four categories. The test ticket is deliberately ambiguous, since it's half billing complaint and half how-to question.\n\n``` python\nimport anthropic\n\nclient = anthropic.Anthropic()\n\nTICKET = \"I got charged twice this month. Also, how do I export my invoices to CSV?\"\n\ndef ask(prompt: str) -> str:\n    response = client.messages.create(\n        model=\"claude-opus-5\",\n        max_tokens=1024,\n        messages=[{\"role\": \"user\", \"content\": prompt}],\n    )\n    return \"\".join(b.text for b in response.content if b.type == \"text\").strip()\n\nweak = f\"Categorize this support ticket: {TICKET}\"\n```\n\nThe weak prompt names no categories, no output format, and no tie-breaking rule, so the model invents all three, differently on every run. That's the failure you're about to fix.\n\n## 3. Zero-shot: state the task completely\n\nZero-shot means no examples, only instructions. It works when the instructions leave nothing to guess: allowed labels, a tie-breaking rule for the ambiguous case, and the exact output shape. The `<ticket>` tags separate untrusted input from your instructions, which Anthropic's docs recommend for any prompt that mixes the two. Append to `prompts.py`:\n\n```\nRULES = \"\"\"You route support tickets. Classify the ticket into exactly one\ncategory: BILLING, BUG, HOW_TO, or FEATURE_REQUEST.\nIf a ticket fits two categories, pick the one where the customer loses\nmoney or data if it's ignored.\"\"\"\n\nzero_shot = f\"\"\"{RULES}\nReply with the category name only.\n\n<ticket>{TICKET}</ticket>\"\"\"\n```\n\n## 4. Few-shot: teach edge cases with examples\n\nFew-shot adds worked examples on top of the instructions. Use it when the format or the judgment calls are easier to show than to describe. Anthropic's guidance: 3 to 5 examples, relevant and diverse, wrapped in `<example>` tags so the model can tell examples from instructions. One example per category also covers the label set. Append:\n\n```\nfew_shot = f\"\"\"{RULES}\nReply with the category name only.\n\n<examples>\n<example>The dashboard shows last week's numbers no matter what date range I pick. -> BUG</example>\n<example>Can you add a dark mode? My eyes hurt at night. -> FEATURE_REQUEST</example>\n<example>Where do I change my notification settings? -> HOW_TO</example>\n<example>My card was declined but you still downgraded my plan. -> BILLING</example>\n</examples>\n\n<ticket>{TICKET}</ticket>\"\"\"\n```\n\n## 5. Chain-of-thought: make the reasoning visible\n\nChain-of-thought asks the model to reason before answering, with tags separating the reasoning from the answer so your code can parse each. Two caveats. Current Claude models already reason internally by default, so this pattern won't raise accuracy much on `claude-opus-5`; its value here is an audit trail you can log when a classification gets disputed. And on models where internal thinking is off, Anthropic's docs recommend exactly this tag structure as the fallback. Append:\n\n```\ncot = f\"\"\"{RULES}\nReason through the classification inside <thinking> tags, then put the\nfinal category (nothing else) inside <answer> tags.\n\n<ticket>{TICKET}</ticket>\"\"\"\n\nfor name, prompt in [(\"weak\", weak), (\"zero-shot\", zero_shot),\n                     (\"few-shot\", few_shot), (\"chain-of-thought\", cot)]:\n    print(f\"--- {name} ---\\n{ask(prompt)}\\n\")\n```\n\n## Verify it works\n\n```\npython prompts.py\n```\n\nExpected output (the weak answer varies between runs; the wording of the thinking varies too, but the three rewrites should land on BILLING every time):\n\n```\n--- weak ---\nThis ticket covers two issues: a billing problem (duplicate charge) and a\nquestion about exporting invoices. I'd categorize it as: Billing / Account...\n\n--- zero-shot ---\nBILLING\n\n--- few-shot ---\nBILLING\n\n--- chain-of-thought ---\n<thinking>The ticket has two parts: a duplicate charge and a CSV export\nquestion. The duplicate charge means the customer is losing money, so the\ntie-breaking rule says BILLING wins over HOW_TO.</thinking>\n<answer>BILLING</answer>\n```\n\nRun it two or three times. The rewrites stay stable; that repeatability is the actual deliverable, since a router that returns prose one run and a label the next can't be parsed downstream.\n\n## Troubleshooting\n\n- **`anthropic.AuthenticationError: Error code: 401 ... 'invalid x-api-key'`** — the key is unset, mistyped, or exported in a different shell. Run`echo $ANTHROPIC_API_KEY` to check, and re-export in the terminal you're running the script from.\n- **`ModuleNotFoundError: No module named 'anthropic'`** — the venv isn't active. Run` source .venv/bin/activate` and confirm`which python` points inside`.venv` .\n- **`anthropic.NotFoundError: Error code: 404 ... 'model: ...'`** — a typo in the model ID. The exact string is` claude-opus-5` , with no date suffix.\n- **`Error code: 400 ... 'Your credit balance is too low to access the Anthropic API'`** — add credits under Plans & billing in the Console; a new account with no balance hits this on the first call.\n\n## Next steps\n\nAnthropic's [prompting best practices](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices) covers the full technique set, including role prompting via the `system` parameter and prompt chaining. From there, the natural upgrades to this router are structured outputs (schema-enforced JSON instead of tag parsing) and a small eval set, because once you have 20 labeled tickets you can measure whether a prompt change helped instead of eyeballing it. The [interactive prompting tutorial](https://github.com/anthropics/prompt-eng-interactive-tutorial) drills each pattern with exercises.\n\n## Sources & further reading\n\n1. \n                                    [Prompting best practices](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/claude-prompting-best-practices)\n                                — platform.claude.com\n2. \n                                    [Get started with Claude](https://platform.claude.com/docs/en/get-started)\n                                — platform.claude.com\n3. \n                                    [Prompt engineering overview](https://platform.claude.com/docs/en/build-with-claude/prompt-engineering/overview)\n                                — platform.claude.com\n4. \n                                    [anthropic - PyPI](https://pypi.org/project/anthropic/)\n                                — pypi.org\n\n[Priya Nair](https://sourcefeed.dev/u/priya_nair)· AI & Developer Experience Writer\n\nPriya covers AI frameworks, developer productivity tooling, and the startup ecosystem across South and Southeast Asia, bringing a researcher's rigour and a practitioner's empathy to every story. She is deeply sceptical of benchmarks and asks hard questions so her readers don't have to.\n\n## Discussion 1\n\nokay but this is testing against one support ticket. how does this actually scale when you've got hundreds of different domain-specific categories and edge cases start piling up? i'd want to see the failure rate and consistency metrics across a realistic dataset before assuming any of these patterns will hold up as a classifier in production.", "url": "https://wpnews.pro/news/rewrite-weak-prompts-with-zero-shot-few-shot-and-chain-of-thought", "canonical_source": "https://sourcefeed.dev/a/rewrite-weak-prompts-with-zero-shot-few-shot-and-chain-of-thought", "published_at": "2026-09-09 11:38:54+00:00", "updated_at": "2026-09-09 12:41:19.912464+00:00", "lang": "en", "topics": ["artificial-intelligence", "large-language-models", "developer-tools"], "entities": ["Priya Nair", "Anthropic", "Claude", "Python"], "alternates": {"html": "https://wpnews.pro/news/rewrite-weak-prompts-with-zero-shot-few-shot-and-chain-of-thought", "markdown": "https://wpnews.pro/news/rewrite-weak-prompts-with-zero-shot-few-shot-and-chain-of-thought.md", "text": "https://wpnews.pro/news/rewrite-weak-prompts-with-zero-shot-few-shot-and-chain-of-thought.txt", "jsonld": "https://wpnews.pro/news/rewrite-weak-prompts-with-zero-shot-few-shot-and-chain-of-thought.jsonld"}}