Ad-Creative Agent: Self-Optimizing Creatives with LangGraph
Generator→Critic loop, synthetic CTR rewards, and Bayesian A/B selection before media spend.
Run a live LangGraph Generator→Critic loop with synthetic CTR rewards, prompt refinement, and Bayesian A/B winner selection for ad creatives.
Primary Features
- Interactive LangGraph Generator → Critic → Prompt Refiner optimization loop.
- Synthetic CTR reward signals scored against historical angle priors.
- Bayesian A/B (Thompson / beta-binomial) winner selection before launch.
- Workflow trace showing inference-time System-2 self-correction steps.
- Campaign brief controls: product, audience, offer, channel, iterations.
- FastAPI + Redis caching pattern ready for App Runner + ElastiCache.
Ad-Creative Agent Playground
Closed-loop revenue engine: LangGraph Generator → Critic (synthetic CTR reward) → Prompt Refiner → Bayesian A/B — before media spend.
Inference-time self-correction
Pipeline: campaign brief → multi-agent loop → synthetic reward gate → Thompson / beta-binomial winner
Part of Module 4 — The Revenue Engine: Self-Optimizing Ad-Creative Agent in the AI Engineering Masterclass.
Tutorial: Building a Self-Optimizing Ad Creative API
What We'll Build - The System Architecture
In this project, we'll implement a self-correcting ad creative agent that generates campaign variants, audits them with a synthetic CTR reward signal, refines the prompt, and returns the best creative before media spend is committed.
You'll build a self-learning digital marketing suite using LangGraph, FastAPI, Redis, GPT-4o/Llama-3, and PostgreSQL production patterns.
The workflow begins when a marketer submits a campaign brief. The generator agent creates a candidate ad, the critic agent scores it against historical CTR patterns, and the prompt refiner updates the next generation instruction. After several iterations, a Bayesian A/B simulator estimates which creative should launch first.

Kernel Labs | Kuriko IWAI | kuriko-iwai.com
Figure A. System architecture — clients and growth tooling enter via gateway/LB; FastAPI serves /optimize and /simulate; LangGraph agents (Generator, Critic, Prompt Refiner, Bayesian A/B) talk to Redis, PostgreSQL, historical CTR priors, and optional LLM providers.

Kernel Labs | Kuriko IWAI | kuriko-iwai.com
Figure B. Closed-loop pipeline — campaign brief → FastAPI → generate/critique/refine loop → Bayesian A/B → winning creative.
Production Goals
- Connect Generative AI directly to business revenue metrics (ROI).
- Implement a self-correcting data flywheel for automated creative iteration.
- Achieve inference-time scaling via multi-agent adversarial debate.
What You'll Master
- Multi-Agent Orchestration with LangGraph
- Closed-loop feedback for continuous model improvement
- Inference-time Scaling (System-2 Thinking)
- Synthetic Reward Signals for CTR Simulation
- Bayesian A/B Testing Simulator for prompt refinement
Essential Source Hooks
1LangGraph orchestration: build_langgraph_workflow in src/workflow.py
2Generator/Critic/Refiner: src/agents.py
3Prompt construction: src/prompts.py
4Synthetic reward: synthetic_reward_signal in src/agents.py
5Bayesian A/B test: bayesian_ab_test in src/agents.py
6
The core loop is intentionally small:
1while True:
2 state = generator_agent(state)
3 state = critic_agent(state)
4 if not should_continue(state):
5 break
6 state = prompt_refiner_agent(state)
7
Shipping AI Systems?
I help teams design and deploy scalable ML / RAG / LLM pipelines and MLOps infrastructure.
Or explore:
- Dive deeper 👉 Research Archive
- Learn by building 👉 AI Engineering Masterclass
- Try it live 👉 Playground
Campaign Brief
The API accepts a structured brief:
1{
2 "product": "AI ad creative optimizer",
3 "audience": "B2B growth teams",
4 "offer": "Book a free CTR audit",
5 "channel": "social",
6 "iterations": 3
7}
8
The core engine converts the request into a CampaignBrief, which carries the product, audience, offer, objective, voice, constraints, and historical performance data.
Generator Agent
The generator chooses an ad angle from historical winners, then creates a candidate headline, body, and call to action. In a production build, this node is where GPT-4o, Llama-3, or another model would generate richer variants.
1candidate = CreativeCandidate(
2 headline=f"{headline_seed}: {brief.product}",
3 body=f"{body_seed} {brief.offer} for {brief.audience}.",
4 call_to_action="Optimize my ads",
5 angle=angle,
6 prompt_version=prompt_version,
7)
8
Critic Agent
The critic produces a synthetic reward signal. It combines historical CTR for the selected angle with simple business heuristics such as offer strength, ROI language, clarity, and constraint penalties.
1reward = predicted_ctr * 1000
2
This gives the system a measurable optimization target before real campaign data arrives.
Prompt Refinement
If the reward is below the configured target, the refiner updates the prompt with the critic's feedback. This creates an inference-time feedback loop: generate, audit, debate the angle, adjust, and generate again.
1Generator -> Critic -> Prompt Refiner -> Generator
2
Shipping AI Systems?
I help teams design and deploy scalable ML / RAG / LLM pipelines and MLOps infrastructure.
Or explore:
- Dive deeper 👉 Research Archive
- Learn by building 👉 AI Engineering Masterclass
- Try it live 👉 Playground
Bayesian A/B Simulation
After candidate generation finishes, the system simulates an A/B test with a beta-binomial Thompson sample. The output includes observed clicks, impressions, posterior CTR samples, and a winning angle.
FastAPI Service
The serving layer exposes five endpoints:
1GET /
2GET /health
3GET /metadata
4GET /simulate
5POST /optimize
6
Repeated optimization requests can be cached in Redis for one hour.
Run the System
Install dependencies:
1uv sync
2
Start the API:
1uv run python app.py
2
Or start with Docker:
1docker-compose up --build
2
Run a simulation:
1curl "http://127.0.0.1:8000/simulate"
2
Smoke Test
Run:
1uv run bash scripts/test.sh
2
This compiles the Python files, verifies API routes, and checks that the optimization loop returns a winning creative.
Architected by Kuriko IWAI

Share What You Learned
Kuriko IWAI, "Ad-Creative Agent: Self-Optimizing Creatives with LangGraph" in Kernel Labs
https://kuriko-iwai.com/labs/ad-creative-agent
Shipping AI Systems?
I help teams design and deploy scalable ML / RAG / LLM pipelines and MLOps infrastructure.
Or explore:
- Dive deeper 👉 Research Archive
- Learn by building 👉 AI Engineering Masterclass
- Try it live 👉 Playground
Continue Your Learning
If you enjoyed this blog, these related entries will complete the picture:
Architecting Semantic Chunking Pipelines for High-Performance RAG
How to Design a Production-Ready RAG System (Architecture + Tradeoffs) (2026 Edition)
Related Books for Further Understanding
These books cover the wide range of theories and practices; from fundamentals to PhD level.

Linear Algebra Done Right

Foundations of Machine Learning, second edition (Adaptive Computation and Machine Learning series)

Designing Data-Intensive Applications: The Big Ideas Behind Reliable, Scalable, and Maintainable Systems

Machine Learning Design Patterns: Solutions to Common Challenges in Data Preparation, Model Building, and MLOps

