# Stream‑Mode AI Agent A lightweight LangChain agent that prints its output **as it is generated** using the `.stream()` API. The project demonstrates how to replace a single `.invoke()` call with streaming so that users see partial responses in real time. > **Why stream?** > When an LLM produces long answers or calls multiple tools, waiting for the whole result can feel like a freeze. Streaming gives instant feedback and improves UX. --- ## 📦 Project Structure ``` . ├── agent.py # Agent definition with streaming support ├── client.py # Simple CLI to interact with the agent └── README.md ``` - **agent.py** – creates an agent that uses a single tool (`get_price`) and streams its output. - **client.py** – runs the agent in a loop, reading user queries from stdin. --- ## ⚙️ Prerequisites | Item | Version | |------|---------| | Python | 3.10+ | | pip | latest | | LangChain | `>=0.2.0` | | Rich | `>=13.0` | > **Tip:** Use a virtual environment to keep dependencies isolated. --- ## 🚀 Installation ```bash # Clone the repo (or copy the files) git clone https://github.com/your-username/stream-agent.git cd stream-agent # Create and activate a venv (optional but recommended) python -m venv .venv source .venv/bin/activate # Windows: .venv\Scripts\activate # Install dependencies pip install --upgrade pip pip install langchain rich ``` > If you plan to use an OpenAI model, set the API key: > ```bash > export OPENAI_API_KEY="sk-..." > ``` --- ## 📖 Usage Run the client script and type your questions. The agent will stream its answer token‑by‑token. ```bash python client.py ``` Example session: ``` $ python client.py Enter your query (Ctrl+C to exit): > What is the price of Bitcoin today? Agent: Checking price... Agent: Current BTC price is $42,300.00 ``` The output appears progressively as the LLM generates it. --- ## 🛠️ Customization - **Add more tools** – decorate a function with `@tool` and pass it to `create_agent`. - **Change model** – modify the `model_name` argument in `agent.py`. - **Adjust streaming format** – tweak the `stream()` call or use Rich’s live rendering for fancy UI. --- ## 📄 License MIT © 2026 ---