Learn / Automation
Building an AI trading bot for prediction markets.
Short answer
A prediction market bot has five parts: market data, a probability model, sizing, order execution and monitoring. The hard part is not the code. It is proving the model beats the price after fees on data it could not have seen, which most AI bots never test.
The five parts of a prediction market bot
Most “AI trading bot for Polymarket and Kalshi” projects describe the same architecture. The parts are not difficult to code. Each one has a way to quietly lose money.
| Component | Job | Common failure |
|---|---|---|
| Market data | Pull order books, trades and market rules via API or WebSocket | Using last-trade prices instead of executable asks; stale data after a disconnect |
| Probability model | Estimate the true chance of each outcome | No evidence it beats the market price after costs |
| Sizing | Decide how many contracts to buy | Betting full Kelly on a noisy estimate |
| Execution | Place, amend and cancel orders | Duplicate orders on retry; ignoring partial fills |
| Monitoring | Track positions, P&L and health | No kill switch when data or the model breaks |
Both Kalshi and Polymarket publish developer APIs for market data and trading. Read their current documentation for authentication, rate limits and terms, and test order code in a demo or paper environment before it touches a funded account.
Step 1: define the edge before writing code
A bot is a way to execute a strategy, not a strategy. Write one sentence explaining why the price should be wrong: a sports model that uses information the market underweights, a venue that reacts slower to news, a cross-platform gap, or a rules nuance others miss. If you cannot write the sentence, the bot will trade noise and pay fees on every trade. Our list of seven strategies and how they fail is a good place to start.
Step 2: the model, and where AI helps
Large language models are genuinely useful in a prediction market pipeline for:
- reading and summarizing settlement rules, and flagging differences between similar markets;
- classifying news and mapping it to the markets it affects;
- drafting strategy code and tests that a human then reviews.
Using an LLM as the probability model itself is where caution is needed. If you ask a model to forecast an election or a game that was played before its training cutoff, it may already “know” the result. A backtest on those events will look brilliant and mean nothing. Only evaluate LLM forecasts on events that resolved after the model’s training data ends, and log the prompt, model version and timestamp for every forecast.
Step 3: size positions with fractional Kelly
For a $1 contract bought at price P when you believe the probability is p, the Kelly criterion gives the bankroll fraction that maximizes long-run growth:
f* = (p − P) / (1 − P)
Include fees by adding the fee per contract to P. Example: your model says 60% and Yes costs 55 cents. Ignoring fees, full Kelly is 11.1% of bankroll. With a Kalshi fee of about 1.74 cents per contract it drops to about 7.5%. Because your 60% is itself uncertain, most traders bet a fraction, such as one-quarter Kelly (about 1.9%), and cap total exposure per event.
import math
def kalshi_fee(contracts: int, price: float, rate: float = 0.07) -> float:
"""Taker fee in dollars, rounded up to the next cent.
Check the current fee schedule; some markets use other rates."""
cents = rate * contracts * price * (1 - price) * 100
return math.ceil(round(cents, 6)) / 100 # round() avoids 175.00000000000003 -> 176
def kelly_fraction(p: float, price: float, fee_per_contract: float = 0.0) -> float:
"""Full-Kelly bankroll fraction for buying a $1 Yes contract."""
cost = price + fee_per_contract
return max(0.0, (p - cost) / (1 - cost))
fee = kalshi_fee(100, 0.55) / 100 # 0.0174 per contract
full = kelly_fraction(0.60, 0.55, fee) # ~0.075
stake = 0.25 * full # quarter Kelly ~1.9% of bankrollStep 4: execution that does not double-fire
- Give every order a client order ID so a network retry cannot create a duplicate.
- Treat partial fills as normal. Reconcile positions from the exchange, not from your own log.
- Prefer limit orders. Market orders in thin books pay the spread and then some.
- Check quote age before trading; skip if data is older than your threshold.
Step 5: monitoring and a kill switch
Set a maximum daily loss, a maximum position per market and a maximum number of orders per minute. Stop trading automatically when any is hit, when data goes stale or when the model returns values outside 0–1. Alert a human. Most bot disasters are not bad predictions; they are loops that kept trading after something broke.
A test plan before real money
- Backtest on point-in-time data. Use the prices and information available at each moment, with fees and spread. See our backtesting guide.
- Hold out a later period. Tune on earlier months; evaluate once on later ones.
- Paper trade live. Record what the bot would have done against real-time books for a few weeks.
- Trade small. Compare live fills with paper fills. The gap is your real execution cost.
- Scale slowly. Larger size moves thin markets against you.
Build it in Kelix Lab
Kelix Lab is our workspace for steps 1 and 2. Research a market in chat with an AI agent, keep the strategy as editable JavaScript beside the conversation, and backtest it on resolved Kalshi markets using quoted bid/ask fills, Kalshi taker fees and hourly volume limits. Strategies only see information available at the simulated time, and later events are reported separately as a held-out sample. Lab does not place live trades.
Frequently asked questions
Can ChatGPT or another LLM trade prediction markets profitably?
Unproven. LLMs are useful for reading rules and news, but backtesting an LLM on past events is contaminated if its training data already contains the outcomes. Test only on events that resolved after the model’s training cutoff.
What position size should a bot use?
Full Kelly for a $1 contract bought at price P with estimated probability p is (p − P) / (1 − P). Because probability estimates are noisy, most practitioners use a fraction such as one-quarter Kelly and cap exposure per event.
Do Kalshi and Polymarket have APIs?
Yes. Both publish developer APIs for market data and orders. Read their current documentation for authentication, rate limits and terms of use before automating anything.
Should I buy a bot from a freelancer?
Be cautious of profit claims. Ask for out-of-sample results after fees with timestamps. A bot without a tested edge only automates losses faster.
Educational content, not financial, legal or tax advice. Prediction markets involve risk, including loss of capital. Examples are illustrative calculations, not live quotes or trading results. Kelix is independent of Kalshi, Polymarket, Robinhood and sportsbooks.