The goal: a working ticker bot

Over the last four lessons, you learned system prompts, structured outputs with Pydantic, and multi-turn chat. Now you'll tie them together into a real, end-to-end project.

In this lesson, you'll build a CLI bot that runs like this:

$ python ticker_bot.py RELIANCE.NS
Briefing for RELIANCE.NS
Price: ₹2,847.50 (↑ 2.3%)
Headlines:
  1. Reliance Energy Division targets ₹50k cr investment
  2. RIL Q1 profit beats analyst expectations
  3. Jio 5G rollout accelerates in Indian metros
Sentiment: Bullish
Summary: Strong fundamentals with positive sector outlook.

Then ask a follow-up: "Why is it up?" — and the bot remembers the briefing data and reasons through it using the multi-turn session from Lesson 04.

The 3-step pipeline: fetch → reason → output

Every agent follows the same pattern. Data comes in messy; you structure it; you send it to the LLM; it returns JSON.

1. Fetch price + news from APIs
→
2. System prompt guides LLM to analyze
→
3. Parse JSON into Pydantic model
📈
Real scenario
Arjun's stock trading assistant (Hyderabad)

Arjun is building a stock tracking app for traders in Hyderabad. Every morning, he wants a one-liner briefing on RELIANCE.NS or HDFCBANK.NS—price snapshot, sentiment, key news. He'll plug this into his Slack bot. The output must be JSON so his backend can save it and resend it.

Step 1: Fetch price data with yfinance

yfinance is a wrapper around Yahoo Finance. No API key needed. It returns real OHLC (Open, High, Low, Close) data for any public ticker.

fetch_prices.py Fetch real price data for Indian and African stocks
RELIANCE.NS
Price: ₹2,847.50
Change: 2.30%

SAFCOM.NR (Safaricom Kenya)
Price: KSh 32.45
Note:
In production, you'd call an API (Alpha Vantage, IEX Cloud, or a custom RSS feed for news). For this lesson, we'll hand-craft a small list of fake but realistic headlines using the real prices.

Step 2: Define the output structure with Pydantic

Before you call the LLM, define exactly what you want back. Use Pydantic (from Lesson 03) to enforce the structure.

ticker_model.py Pydantic model for structured briefing output
Why Pydantic?
If the LLM forgets a field or mislabels sentiment (e.g., "BULLISH" instead of "bullish"), Pydantic catches it before it crashes your app.

Step 3: System prompt that generates JSON

The system prompt tells the LLM: "You are an agent that analyzes tickers. Return only valid JSON matching this schema."

ticker_system_prompt.py System prompt for structured ticker analysis

Putting it together: end-to-end ticker bot

Here's the complete, runnable CLI bot that fetches data, prompts the LLM, and saves the briefing to JSON.

ticker_bot.py Complete ticker briefing bot with multi-turn follow-ups
$ python ticker_bot.py RELIANCE.NS

Fetching briefing for RELIANCE.NS...

Briefing for RELIANCE.NS
Price: ₹2,847.50 (+2.30%)
Headlines:
  1. Reliance Energy Division targets ₹50k cr investment
  2. RIL Q1 profit beats analyst expectations
  3. Jio 5G rollout accelerates in Indian metros
Sentiment: Bullish
Summary: Strong fundamentals with positive sector outlook.

Saved to briefings/2026-04-16-RELIANCE.NS.json

Extend it: multi-turn Q&A using ChatSession

Once you have the briefing, use the ChatSession from Lesson 04 to answer follow-up questions. The bot remembers the ticker data across turns.

ticker_chat.py Multi-turn chat using the briefing as context
Bot: The stock is up due to strong Q1 earnings that beat expectations and the accelerating 5G rollout, which drives future revenue.

Bot: RELIANCE is an energy giant with diversified revenue (oil, gas, retail, digital). HDFCBANK is a financial services play. Both are strong performers, but they're in different sectors.

Bot: Based on strong fundamentals and positive sector outlook, RELIANCE looks like a hold if you own it; a buy if you're looking to accumulate over time.

Stretch: build a mini archive and analytics dashboard

Save briefings to a timestamped JSON archive. Then build a simple dashboard that reads all briefings and shows trends.

Exercise idea:
Write a script that reads all files in `briefings/`, computes average sentiment over the last 7 days, and plots price trends for each ticker. Hint: use `glob.glob("briefings/*.json")` to list all saved briefings.

How real agents work

This ticker bot is a tiny agent. In production, agents are far more complex: they call APIs (Stripe for payments, Slack for notifications), maintain persistent databases, and reason through multi-step tasks. But the core pattern never changes: data → LLM → structured output. Every AI system from financial trading bots to medical diagnostic tools to customer service agents follows this 3-step pipeline. You've built the real thing.

Pipelines Structured outputs LLM-powered agents System prompts JSON persistence
🎯 Mini Challenge: Build a TCS ticker briefing

Task: Run the ticker bot for TCS.NS (Tata Consultancy Services). Then ask a follow-up question using ChatSession: "Is TCS a good long-term hold given its growth in digital services?"

Verify that:

1. The bot fetches real price data for TCS.NS

2. The structured JSON includes a bullish/bearish/neutral sentiment

3. The ChatSession remembers TCS context in the follow-up

Hint: Run `python ticker_bot.py TCS.NS`, then create a ChatSession with the returned briefing object. Use the briefing data as context in the system prompt, then call `session.chat("Is TCS a good long-term hold...")`. The bot will reference the briefing context automatically.

Test your knowledge

Question 1 of 5
What are the three main steps in the ticker briefing pipeline?
Question 2 of 5
Why use Pydantic for the output model?
Question 3 of 5
What does the system prompt tell the LLM in this ticker bot?
Question 4 of 5
How does ChatSession enable multi-turn questions about the briefing?
Question 5 of 5
How would you extend this bot to support comparing two tickers in a follow-up?
0
/5
Excellent! You've mastered the agentic pipeline.
Africa & India's Best AI Academy
🤖 AI-first 🎓 Placement Assistance

You've built a real agent.
Now scale it to production systems.

Join the Best AI Program: The AI-Era Agentic Engineer — 5 months, fully online, with placement assistance. Week 1 is free.

Explore The AI-Era Agentic Engineer →