Files

2.4 KiB
Raw Permalink Blame History

StreamMode AI Agent

A lightweight demo of a LangChain agent that streams its output tokenbytoken instead of waiting for the entire response.
The project showcases how to replace a single .invoke() call with .stream(), so you can see the answer appear in real time.


📖 Description

  • Stack: LangChain, create_agent, @tool, rich
  • Files:
    • agent.py defines tools and creates an agent that streams its output.
    • client.py simple CLI to interact with the agent.

The agent can call external tools (e.g., a calculator) while streaming, making it feel more interactive for long answers or multistep reasoning.


⚙️ Prerequisites

Item Version
Python ≥ 3.10
pip Latest
Virtual environment (recommended) Any

The project uses the following packages:

langchain==0.2.*          # LangChain core
rich==13.*                # Pretty console output

🚀 Installation

  1. Clone the repository

    git clone https://github.com/your-username/stream-ai-agent.git
    cd stream-ai-agent
    
  2. Create a virtual environment (optional but recommended)

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

    pip install -r requirements.txt
    

If you don't have a requirements.txt, create one with:

langchain>=0.2,<0.3
rich>=13,<14

🏃‍♂️ Running the Agent

python client.py

You will see a prompt:

Enter your question (or type 'exit' to quit):

Type any query, e.g.:

What is 12 * 8?

The agent will stream its answer token by token, and if it needs to use the calculator tool, youll see the intermediate tool call printed in real time.


📌 Example Session

$ python client.py
Enter your question (or type 'exit' to quit): What is 12 * 8?

> Calculating...
> 96
Answer: 96

The > lines are produced by the rich library, showing tool calls and partial outputs as they arrive.


🔧 Extending the Agent

  • Add more tools: Decorate a function with @tool(name="...", description="...").
  • Change LLM: Pass a different model to create_agent(..., llm=...).
  • Custom streaming logic: Override the default stream handling in agent.py.

📄 License

MIT © 2026

---```