This tutorial shows how to build an AI agent by using the ReAct (reasoning and acting) framework and the IBM® Granite® 4.0 Nano language model. The agent uses prompt engineering to combine reasoning steps with an action plan. It processes news URLs, retrieves data from external sources and generates a structured financial analysis in JSON format. This approach ensures clear, transparent and grounded results.
Before we jump into the code, let’s understand some core concepts.
The ReAct framework enhances large language models (LLMs) by combining internal reasoning with external actions. It integrates reasoning (chain-of-thought, or CoT) and acting (tool use) to create an effective ReAct agent capable of dynamic decision-making.
ReAct’s benefits: It grounds the model in verified external data rather than internal training memory, improving transparency and supporting reliable decision-making in complex tasks.
We use the IBM Granite 4.0 Nano model family, specifically the 1B-instruct variant. This AI model is fine tuned for reasoning and instruction-following tasks, making it suitable for building intelligent agents.
The Granite 4.0 Nano model demonstrates how compact, fine tuned models can achieve strong performance and interpretability similar to larger commercial AI models.
Financial news is rarely straightforward. Articles often mix conflicting signals. For example, a company might report strong sales (positive signal) but its stock can fall due to weak guidance or higher expenses (negative reaction).
A single-step summary cannot capture this nuance. The ReAct agent improves question answering and reasoning by working step by step, combining reasoning and action in interleaved trajectories. It uses in-context examples and fact verification to ensure that each action step aligns with reliable real-world information.
The agent converts raw news text into structured financial intelligence. It delivers results as one DataFrame object by using Python-based workflows built on machine learning and retrieval techniques like RAG (retrieval-augmented generation).
Classification:
The outputs are instantly consumable by downstream systems and comparable to benchmarks such as HotpotQA or other baseline datasets. This approach supports accurate, real-time financial insight and a grounded final answer.
You can execute this tutorial on your local machine by using your own Python and Jupyter environment. The local setup gives you full control and flexibility. To run this tutorial and to execute the notebook on your local Windows or macOS machine, you must ensure the following setup.
To run the Granite 4.0 Nano model and ReAct workflow, you need the core libraries that handle model loading and computation. Installing them ensures that your environment can execute reasoning and action steps smoothly.
This step loads the Granite 4.0 Nano instruction‑tuned model and its tokenizer from Hugging Face. The tokenizer converts text to tokens, and the model generates responses. The code automatically selects a GPU where available, otherwise it runs on CPU.
Note: Model loading takes about 2–4 minutes on 8 GB RAM and 1–2 minutes on 16 GB RAM; if you see an “out of memory” error on 8 GB systems, restart the kernel and close other applications before retrying.
This step creates a small dataset of five financial news summaries used to test the ReAct agent. The articles are drawn from different sectors—biotechnology (Lenz Therapeutics), payments (Shift4 Payments), lidar technology (Luminar), electric vehicles (Lucid) and consumer goods (Celsius).
Each article includes mixed market signals such as strong earnings but weak guidance, higher costs or regulatory challenges. This mix helps evaluate how the agent handles conflicting information and classifies sentiment accurately.
This step defines a simple function, web_browser(), that simulates how the ReAct agent retrieves text from an external source. It looks up the article text for a URL from the dataset.
This step defines the prompt that guides the agent’s reasoning. The template enforces the ReAct pattern—thought to action to observation—and specifies how the model should use the web_browser() tool and format its final output as structured JSON.
This function prepares the initial ReAct prompt, performs the tool call and returns all intermediate components for the next step.
This function calls the precedeing setup function, performs model generation and safely parses the output.
This step runs the ReAct agent on one example URL to verify the workflow and ensure that the model generates a valid JSON response before processing multiple cases.
Output:
Testing single ReAct run...
Prompt snippet:
You are an expert financial analyst agent. Your task is to analyze the provided financial news article URL. Follow the ...
Observation snippet:
Luminar faces SEC investigation, missed payments, CFO exit, and workforce cuts. Bankruptcy risk rising amid liquidi...
Prompt length: 1795 chars | Token count: 400
--- Model Output ---
systemYou are an expert financial analyst agent following the ReAct framework. Analyze the observation and produce the output in this exact format:
Classification:
Sentiment: Positive/Negative/Mixed/Neutral
Topic: Earnings/Regulatory/Product/Operations/M&A/HR/Layoffs/Supply Chain
Summary:
- Key takeaway 1
- Key takeaway 2
- Key takeaway 3
Start with Classification, then Summary. Do not include any extra text or role markers. Base sentiment and topic ONLY on the observation text. Use actual values from the article.
user
You are an expert financial analyst agent. Your task is to analyze the provided financial news article URL. Follow the ReAct framework (Thought -> Action -> Observation) until you produce the final structured JSON analysis.
**AVAILABLE TOOLS:**
web_browser(url: str) -> str: Fetches the full textual content of the article.
**FINAL OUTPUT INSTRUCTIONS:**
Return a single JSON object with:
1. "Classification": { "Sentiment": Positive/Negative/Mixed/Neutral, "Topic": Earnings/Regulatory/Product/Operations/M&A/HR/Layoffs/Supply Chain }
2. "Summary": A concise bulleted list of 3-5 key takeaways.
---
**INPUT ARTICLE URL:** https://www.photonics.com/Articles/Amid-SEC-Investigation-Luminar-Cuts-Workforce/p5/a71615
---
**START ANALYSIS:**
Thought: Retrieve article content using web_browser tool.
Action: web_browser(url='https://www.photonics.com/Articles/Amid-SEC-Investigation-Luminar-Cuts-Workforce/p5/a71615')
Observation:
Luminar faces SEC investigation, missed payments, CFO exit, and workforce cuts. Bankruptcy risk rising amid liquidity issues.
Thought: Proceed with classification and summarization.
Action:
assistantClassification:
Sentiment: Negative
Topic: Operations
Summary:
- Luminar faces a significant SEC investigation due to missed payments.
- The company has experienced a CFO exit, indicating internal financial challenges.
- Luminar has implemented workforce cuts to address liquidity issues.
- The company is facing bankruptcy risk due to its financial struggles.
This cell applies the ReAct agent to all case study URLs, aggregates the results into a structured DataFrame and displays the sentiment, topic and summary for each company.
Output:
--- Starting Batch ReAct Analysis ---
Analyzing: Article 1: Luminar...
Analyzing: Article 2: Celsius...
Analyzing: Article 3: Lucid...
Analyzing: Article 4: Shift4...
Analyzing: Article 5: Lenz...
--- Batch Analysis Results ---
| Company/Case | Sentiment | Topic | Summary | |
|---|---|---|---|---|
| 0 | Article 1: Luminar | Negative | Operations | Luminar faces a significant SEC investigation due to missed payments.\n- The company has experienced a CFO exit, indicating internal financial challenges.\n- Luminar has implemented workforce cuts to address liquidity issues.\n- The company is facing bankruptcy risk due to its financial struggles. |
| 1 | Article 2: Celsius | Negative | Earnings | Celsius reported EPS of 725.1M, also exceeding expectations.\n- Stock price fell 17.5% following the earnings call. |
| 2 | Article 3: Lucid | Negative | Operations | Lucid missed Q3 revenue targets, resulting in a 2.65 per share.\n- Production forecasts were reduced due to supply chain challenges. |
| 3 | Article 4: Shift4 | Positive | Earnings | EPS beat expectations with 589.2M |
| 4 | Article 5: Lenz | Negative | Earnings | Q3 2025 financial results show a net loss of 10 million in milestone payments reflect successful clinical progress.\n- Operating expenses rose 44% due to increased SG&A costs. |
This tutorial demonstrated how to build a financial insight agent by using the ReAct framework and the IBM Granite 4.0 Nano language model. You learned how the agent performs reasoning and action steps, retrieves external data through a simulated lookup tool and produces structured financial summaries.
The ReAct workflow helps reduce AI hallucination by grounding each decision in retrieved information rather than relying only on internal memory. By combining transparent reasoning traces with tool use, the agent provides reliable, auditable financial insights.
Next steps
You can extend this tutorial by connecting the agent to live financial APIs or a retrieval‑augmented generation (RAG) pipeline for real‑time market analysis.
Running the model on IBM watsonx.ai® improves speed and scalability, while custom prompts or domain‑specific fine‑tuning can further enhance performance for specialized financial use cases.
Easily design scalable AI assistants and agents, automate repetitive tasks and simplify complex processes with IBM® watsonx Orchestrate™.
Move your applications from prototype to production with the help of our AI development solutions.
Reinvent critical workflows and operations by adding AI to maximize experiences, real-time decision-making and business value.