2026-05-28 14:22:08 +00:00
2026-05-28 10:37:43 +00:00
2026-05-28 14:14:15 +00:00
2026-05-28 14:14:14 +00:00

StreamMode AI Agent

A lightweight LangChain agent that streams its responses token by token using the rich library for pretty console output.

Why stream?
When an LLM generates a long answer or calls multiple tools, waiting until the entire response is ready can feel like a freeze. Streaming lets you see the answer as it is produced, improving interactivity and user experience.


📦 Features

  • Uses LangChain for agent orchestration
  • Powered by OpenAI GPT4o-mini (or any OpenAI model)
  • Realtime streaming via llm.stream(...)
  • Pretty console output with rich
  • Simple, singlefile implementation (solution.py)

⚙️ Prerequisites

Item Minimum Version
Python 3.10+
pip
OpenAI API key Set as environment variable OPENAI_API_KEY

Tip: Create a virtual environment before installing dependencies.

python -m venv .venv
source .venv/bin/activate   # On Windows: .\.venv\Scripts\activate

📥 Installation

# Clone the repo (or copy solution.py into your project)
git clone https://github.com/<your-username>/stream-ai-agent.git
cd stream-ai-agent

# Install dependencies
pip install -r requirements.txt

requirements.txt:

langchain==0.2.*
openai==1.*
rich==13.*

Adjust the version numbers if you prefer newer releases.


🚀 Running the Agent

export OPENAI_API_KEY="sk-..."   # On Windows: set OPENAI_API_KEY=sk-...
python solution.py

The script will prompt you for a question. Type your query and press Enter.
Youll see the answer appear token by token in real time.


📄 Example

$ python solution.py
🤖  What is the capital of France?
🇫🇷  Paris

The agent will stream each word (or token) as it arrives, giving a smooth typinglike effect.


🔧 Customization

  • Change model edit model="gpt-4o-mi" in solution.py.
  • Add tools decorate functions with @tool and include them in the agent.
  • Adjust temperature modify temperature=0 to a higher value for more creativity.

📚 Further Reading


Happy streaming! 🚀

S
Description
Решение: Stream-режим AI-агента
Readme 42 KiB
Languages
Python 100%