How I Built an AI System That Learns Where to Spend Marketing Dollars
The $500 Question That Started Everything
How I Built an AI System That Learns Where to Spend Marketing Dollars
The $500 Question That Started Everything
Last month, a friend who runs a small e-commerce store asked me a simple question: “I have $500 to spend on marketing this week. Should I target young professionals, parents, or retirees?”
I started to give the usual advice about A/B testing, but then I stopped. Traditional A/B testing takes weeks, wastes money on underperforming segments, and by the time you have results, the market has often changed. There had to be a better way.
That conversation led me to build something I’m excited to share today: a system that automatically learns which customer segments perform best and adjusts budget allocation in real time. No more guessing. No more waiting weeks for test results. Just intelligent, adaptive optimization.
The Problem With How We Currently Allocate Marketing Budgets
Most businesses handle budget allocation in one of three ways:
- The Gut Feel Method: “Young professionals seem to buy more, so let’s give them 60% of the budget”
- The Even Split: “Let’s be fair and give everyone 33.3%”
- The Last Month Method: “Whatever worked last month, let’s do that again”
All three approaches share the same fatal flaw: they don’t learn. When young professionals suddenly stop converting because a competitor launched a targeted campaign, these methods keep throwing money at them until someone notices the monthly report.
Enter Thompson Sampling: The Casino Algorithm That Solves Marketing
Here’s where things get interesting. There’s a mathematical problem called the “multi-armed bandit” that casinos have been dealing with for decades. Imagine you’re in a casino with ten slot machines. Each machine has different odds, but you don’t know what they are. How do you figure out which machine is best while maximizing your winnings?
This is exactly our marketing problem. Each customer segment is like a slot machine with unknown conversion rates. We need to figure out which segments convert best (exploration) while spending most of our money on the winners (exploitation).
Thompson Sampling, developed in 1933, solves this elegantly. Here’s how it works in plain English:
- Start with uncertainty: “I think young professionals convert at 2%, but I’m not sure”
- Take calculated risks: “Let me spend a bit more on parents today to test if they’re actually better”
- Update beliefs: “Parents converted at 3% today. Maybe they’re better than I thought”
- Gradually converge: “After 7 days, I’m confident parents convert best. Give them 60% of the budget”
The Math Behind the Magic (Without the PhD Required)
The beauty of Thompson Sampling lies in how it represents uncertainty. For each customer segment, the system maintains two numbers:
- Alpha: How many times this segment has converted (successes)
- Beta: How many times this segment didn’t convert (failures)
These create what’s called a Beta distribution, which is just a fancy way of saying “here’s what I think the conversion rate is, and here’s how confident I am.”
When a segment is new (low Alpha and Beta), the system is uncertain, so it varies the budget more to learn. When a segment has lots of data (high Alpha and Beta), the system is confident and allocates budget based on proven performance.
Every day, the system:
- Randomly samples from each segment’s distribution
- Allocates budget proportional to these samples
- Observes actual performance
- Updates its beliefs
It’s like having a marketing analyst who never sleeps, constantly running experiments and updating strategies.
Building the System: From Theory to Dashboard
I wanted this to be more than just an algorithm. Marketing teams need to see what’s happening and understand why. So I built a complete dashboard with multiple interconnected components working together.
But first, let me show you the complete architecture:

How the System Architecture Works
Looking at the diagram above, you can see how data flows through seven distinct stages. Let me walk you through what happens when you start the optimization process.
Step 1: Load Historical Data Everything begins with your historical campaign data stored in facts_daily.parquet. This file contains the gold mine of information: which customer segments clicked your ads, who converted, and how much you spent on each group. The Rate Prep module (rates.py) then calculates the key metrics: Cost Per Click (CPC) medians and Conversion Rate (CVR) priors for each segment. Think of this as the system getting its bearings before making any decisions.
Step 2: Initialize the Brain (Thompson Sampling Engine) The green box in the diagram is where the magic happens. The Thompson Sampling Engine has three core components:
state.py: Maintains Beta distributions (those Alpha and Beta values) for each segmentts.py: Samples from these distributions to decide today's budget splitupdate.py: Learns from results by updating Alpha (successes) and Beta (failures)
This is the learning heart of the system. Each segment gets its own “belief model” that becomes more accurate over time.
Step 3: Daily Sample and Allocate Every simulated day, the engine samples from each segment’s distribution. If it thinks Parents convert at 3% but isn’t sure, it might sample 2.5% one day and 3.5% another. This controlled randomness is how the system explores. Budget gets allocated proportionally to these samples, with safety rails ensuring no segment gets completely abandoned.
Step 4: Simulate Outcomes The pink Simulation box (simulate.py) is your safety net. Instead of spending real money, it uses realistic CPC/CVR rates and binomial sampling to predict what would happen with the new budget allocation. It's like a flight simulator for your marketing spend. The results feel real because they include the natural randomness of actual campaigns.
Step 5: Update Beliefs After each simulated day, the system observes what happened. Did Parents actually convert at 3.5%? Great, increase their Alpha. Did they underperform? Increase Beta instead. This feedback loop is continuous. The system literally gets smarter every day.
Step 6: AI Analysis Once the 7-day simulation completes, two specialized AI agents jump in:
- Trends Agent: Performs deep performance analysis, spots anomalies, and generates human-readable insights using LLMs
- Planner Agent: Creates specific budget recommendations, assesses risk levels, and provides business rationale for changes
These agents transform raw numbers into actionable insights like “Parents segment shows weekend spikes, consider day-parting your campaigns.”
Step 7: Dashboard Display The yellow FastAPI dashboard is where everything comes together. The Interactive UI shows real-time progress bars and visualizations. The REST API endpoints (/start-ts and /start-agents) handle user interactions. Background tasks ensure the UI stays responsive even during complex calculations. And comprehensive error handling keeps things running smoothly.
The final outputs are clean and actionable:
plan.csv: The optimized budget allocation for each dayinsights.json: AI-generated analysis and patternssuggestions.json: Specific recommendations with reasoning
The Data Flow That Ties It All Together
Notice the arrows in the diagram? They show the elegant data flow:
- Historical data loads into memory
- Arms (segments) get initialized with prior beliefs
- Daily sampling and allocation happens
- Simulation generates realistic outcomes
- Beliefs update based on results
- AI analyzes the complete picture
- Dashboard displays everything beautifully
This isn’t just a linear pipeline. The system has feedback loops (see those bidirectional arrows between Thompson Sampling and Simulation). Each day’s results influence the next day’s decisions. It’s a living system that adapts continuously.
The modular design means you can upgrade individual components without breaking others. Want to try a different bandit algorithm? Swap out the Thompson Sampling module. Need different visualizations? Update the dashboard. It’s built to evolve with your needs.
Now let’s dive into each component:
1. The Learning Engine
This is the Thompson Sampling core. Feed it your historical data (clicks, conversions, spend by segment), and it immediately starts learning. I added safety rails too:
- Minimum 3% budget per segment (never completely abandon a segment)
- Maximum 25% daily change (no wild swings that could tank performance)
2. The Simulation Environment
Before risking real money, the system runs simulations using your historical data. It shows you exactly how the budget would have been allocated over the past week and what the results would have been. This builds confidence before going live.
3. The AI Analyst
This was the fun part. I integrated GPT-4 to analyze the optimization results and provide human-readable insights. Instead of staring at charts, you get explanations like:
“The Parents segment showed a 47% improvement in conversion rate between Day 3 and Day 5, likely due to the weekend effect. The algorithm correctly identified this pattern and increased allocation from 20% to 35%. Consider creating parent-specific weekend campaigns.”
Real Results From the Simulation
Running the system on actual e-commerce data, here’s what happened over 7 days:
Day 1: Even split across all segments (33% each) while learning Day 3: System identifies “Parents” segment performing 2x better Day 5: Budget shifts to 45% Parents, 35% Young Professionals, 20% Retirees Day 7: Optimization stabilizes with 58% improvement in overall conversion rate
The key insight? The system discovered that parents converted best on weekends, while young professionals converted better on weekdays. A human analyst might have spotted this eventually, but the algorithm found it in 3 days.
The Unexpected Discovery: AI Agents as Business Analysts
Initially, I added the AI insights feature as a nice-to-have. But it became the most valuable part. The AI agents do three things remarkably well:
- Pattern Recognition: “Retirees show increasing engagement after 3 PM, suggesting daytime targeting”
- Anomaly Detection: “Unusual spike in Young Professionals on Day 4 coincides with tech news coverage”
- Strategic Recommendations: “Consider increasing overall budget by 20% given the 3.2% conversion rate exceeds industry benchmarks”
It’s like having a senior data analyst review your campaigns, except it happens in seconds, not days.
Practical Lessons for Implementation
If you’re thinking about building something similar, here are the hard-won lessons:
Start Simple
Don’t try to optimize 50 segments on day one. Start with 3–5 clear segments and expand once the system proves itself.
Set Guard Rails
Always include minimum and maximum allocation limits. Markets can be unpredictable, and you don’t want the algorithm putting all eggs in one basket.
Historical Data Matters
The system learns faster with good historical data. Even 30 days of past performance dramatically improves initial predictions.
Explain the Magic
Your marketing team needs to trust the system. Show them the learning process, not just the final numbers. The dashboard’s progress bars and real-time updates were crucial for buy-in.
What This Means for Small Businesses
The most exciting part? This isn’t just for companies with million-dollar budgets. The same friend who inspired this project is now using it for his $500 weekly budget. The system works identically whether you’re optimizing $500 or $50,000.
Small businesses can now access the same sophisticated optimization that large corporations use, without hiring a team of data scientists. The entire system runs on a basic laptop and costs nothing beyond the OpenAI API calls (about $2 per month for a small business).
The Code is Yours
I’ve open-sourced the entire project. You can grab it, modify it for your business, and start optimizing today. The setup takes about 10 minutes:
- Install Python and dependencies
- Add your OpenAI API key
- Load your historical data (or use the sample data)
- Run the dashboard
The system includes everything: the Thompson Sampling engine, the simulation environment, the web dashboard, and the AI analysis agents.
Looking Forward: Where This Could Go
This project scratched the surface of what’s possible. Here are some extensions I’m considering:
Multi-channel optimization: Not just customer segments, but also platforms (Google, Facebook, TikTok) Creative testing: Apply the same learning to ad creatives and messaging Lifetime value optimization: Optimize for long-term customer value, not just immediate conversions Real-time bidding: Connect directly to ad platforms for automated budget adjustment
The Bigger Picture
We’re entering an era where AI doesn’t just analyze data; it actively learns and optimizes business operations. Thompson Sampling is 90 years old, but combining it with modern AI creates something powerful: systems that learn, adapt, and explain themselves.
Marketing budget allocation is just the beginning. The same principles apply to inventory management, pricing strategies, content recommendation, and dozens of other business decisions.
Your Turn
If you’re tired of making budget decisions based on gut feel or outdated reports, give this a try. The code is ready, the math is proven, and the results speak for themselves.
Start with your smallest campaign. Let it run for a week. Watch it learn. Then ask yourself: what other business decisions could benefit from this kind of intelligent automation?
The future of marketing isn’t about bigger budgets or cleverer campaigns. It’s about systems that learn faster than your competition. And now you have one.
Want to try it yourself? Check out the GitHub repository for complete code and documentation. Questions or improvements? I’d love to hear about your experience. Drop a comment below or reach out directly.
If you found this helpful, follow for more articles on practical AI applications in business. Next week: How I used reinforcement learning to optimize customer support ticket routing.
메타데이터
- post_id
- 0eaa25e7f19e
- slug
- how-i-built-an-ai-system-that-learns-where-to-spend-marketing-dollars-0eaa25e7f19e
- url
- https://medium.com/@joshi.tanm/how-i-built-an-ai-system-that-learns-where-to-spend-marketing-dollars-0eaa25e7f19e
- canonical_url
- https://medium.com/@joshi.tanm/how-i-built-an-ai-system-that-learns-where-to-spend-marketing-dollars-0eaa25e7f19e
- author_url
- https://medium.com/@joshi.tanm
- status
- ok
- fetched_at
- 2026-06-09 15:37:30