# Telegram Bot LangGraph Integration for Mobile Access: Architecture and Implementation Guide

> Learn how to integrate your Telegram bot with LangGraph for mobile access. Understand the architecture and implementation of a lightweight RAG pipeline powered by FastAPI.

- Repository: [jamwithai/production-agentic-rag-course](https://github.com/jamwithai/production-agentic-rag-course)
- Tags: architecture
- Published: 2026-03-23

---

**The Telegram bot operates independently of LangGraph through a lightweight RAG pipeline, while LangGraph powers the separate Agentic RAG service available via FastAPI endpoints that mobile clients can consume.**

The production-agentic-rag-course repository implements a dual-runtime architecture for arXiv paper question-answering, cleanly separating lightweight mobile responses from advanced agentic reasoning. While the Telegram bot delivers fast answers through direct OpenSearch retrieval and Ollama inference, the LangGraph-powered Agentic RAG service provides sophisticated guard-rails and automatic query rewriting for complex research workflows.

## Current Telegram Bot Architecture (Simple Runtime)

The Telegram bot follows a simplified retrieval pipeline optimized for mobile responsiveness. When the FastAPI application starts, the service is constructed by [`src/services/telegram/factory.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/telegram/factory.py) and launched within the `lifespan` context manager of [`src/main.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/main.py).

### Service Initialization

The startup sequence wires together OpenSearch, embeddings, Ollama, and caching clients:

```python

# src/main.py – service startup within lifespan

telegram_service = make_telegram_service(
    opensearch_client=app.state.opensearch_client,
    embeddings_client=app.state.embeddings_service,
    ollama_client=app.state.ollama_client,
    cache_client=app.state.cache_client,
    langfuse_tracer=app.state.langfuse_tracer,
)
if telegram_service:
    await telegram_service.start()

```

### Query Processing Pipeline

All message handling occurs inside `TelegramBot._handle_question`. The current implementation executes four distinct steps without LangGraph involvement:

1. **Cache lookup** – checks for existing responses to avoid redundant computation
2. **Embedding generation** – creates query vectors when hybrid search is enabled  
3. **OpenSearch retrieval** – executes unified hybrid search across paper chunks
4. **Ollama inference** – generates answers using the `RAGPromptBuilder`

```python

# src/services/telegram/bot.py – _handle_question method

query = update.message.text
ask_request = AskRequest(query=query, top_k=3, use_hybrid=True)

# Optional cache lookup

if self.cache:
    cached_response = await self.cache.find_cached_response(ask_request)
    if cached_response:
        await self._send_answer(update, cached_response)
        return

# Generate embedding for hybrid search

if ask_request.use_hybrid:
    query_embedding = await self.embeddings.embed_query(query)

# Search OpenSearch

search_results = self.opensearch.search_unified(
    query=query,
    query_embedding=query_embedding,
    size=ask_request.top_k,
    use_hybrid=ask_request.use_hybrid and query_embedding is not None,
)

# Build prompt and call Ollama

prompt = RAGPromptBuilder().create_rag_prompt(query=query, chunks=chunks)
ollama_response = await self.ollama.generate(
    model="llama3.2:1b", 
    prompt=prompt, 
    stream=False
)
answer = ollama_response.get("response", "")

```

## Understanding the LangGraph Agentic RAG Service

The repository maintains LangGraph strictly within the **Agentic RAG service** ([`src/services/agents/agentic_rag.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/agents/agentic_rag.py)). This implementation orchestrates complex workflows including guard-rails, document grading, query rewriting, and conditional retrieval loops.

The service is exposed through the FastAPI router at [`src/routers/agentic_ask.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/routers/agentic_ask.py) via the `/api/v1/agentic_ask` endpoint, making it accessible via HTTP rather than direct Python imports from the Telegram module.

## Integration Strategies for Mobile Access

To enable **LangGraph-driven reasoning** from Telegram mobile clients, you have three architectural options:

### Method 1: HTTP API Endpoint (Zero Code Changes)

The simplest approach calls the existing `/api/v1/agentic_ask` endpoint from within the bot handler:

```python
import httpx

async def call_agentic_ask(query: str):
    async with httpx.AsyncClient() as client:
        resp = await client.post(
            "http://localhost:8000/api/v1/agentic_ask",
            json={"query": query, "user_id": "telegram_user"},
        )
        data = resp.json()
        return data["answer"], data["sources"]

```

### Method 2: Direct Service Injection

Modify [`src/services/telegram/factory.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/telegram/factory.py) to pass the `AgenticRAGService` instance to the bot constructor, then replace the Ollama call:

```python

# New import in bot.py

from src.services.agents.agentic_rag import AgenticRAGService

# Extended constructor signature

def __init__(self, ..., agentic_rag: AgenticRAGService | None = None):
    self.agentic_rag = agentic_rag

# Replacement for simple Ollama call inside _handle_question

if self.agentic_rag:
    result = await self.agentic_rag.ask(
        query, 
        user_id=update.effective_user.id
    )
    answer = result["answer"]
    sources = result["sources"]

```

### Method 3: Independent Mobile Client

Mobile applications can bypass the simple bot entirely and call the FastAPI endpoint directly, treating the Telegram interface as a thin client that proxies requests to the LangGraph backend.

## Summary

- The Telegram bot in [`src/services/telegram/bot.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/telegram/bot.py) implements a **lightweight RAG pipeline** without LangGraph dependencies, using direct calls to `self.ollama.generate()`
- LangGraph orchestration lives exclusively in [`src/services/agents/agentic_rag.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/agents/agentic_rag.py) and exposes functionality via [`src/routers/agentic_ask.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/routers/agentic_ask.py) at `/api/v1/agentic_ask`
- Mobile access to LangGraph features requires either **HTTP API calls** to the agentic endpoint or **dependency injection** of `AgenticRAGService` into the bot factory
- The `TelegramBot._handle_question` method currently follows a four-step workflow: cache check, embedding generation, OpenSearch hybrid search, and Ollama inference

## Frequently Asked Questions

### Does the Telegram bot use LangGraph by default?

No. According to the source code in [`src/services/telegram/bot.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/telegram/bot.py), the bot executes a simple pipeline of embedding generation, OpenSearch retrieval, and direct Ollama inference. The LangGraph workflow resides in a separate service ([`src/services/agents/agentic_rag.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/agents/agentic_rag.py)) that is not imported or called by the default Telegram implementation in the production-agentic-rag-course repository.

### How do I enable LangGraph features in the Telegram bot?

You must either inject the `AgenticRAGService` class into the bot's constructor via [`src/services/telegram/factory.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/services/telegram/factory.py), or modify `TelegramBot._handle_question` to POST requests to the `/api/v1/agentic_ask` FastAPI endpoint. The factory already handles `langfuse_tracer` injection, providing a clear pattern for adding the agentic service.

### What are the performance differences between the two runtimes?

The simple bot delivers lower latency through direct OpenSearch queries and single-pass Ollama generation. The LangGraph runtime incurs additional overhead from guard-rail checks, query rewriting loops, and document grading nodes, but produces higher-quality answers for complex research questions requiring multi-step reasoning.

### Can mobile clients access both runtimes simultaneously?

Yes. The FastAPI application in [`src/main.py`](https://github.com/jamwithai/production-agentic-rag-course/blob/main/src/main.py) initializes both services concurrently through the lifespan context manager. Mobile clients can default to the lightweight Telegram bot for quick answers while offering a "deep research" mode that calls the `/api/v1/agentic_ask` endpoint to trigger the LangGraph workflow with guard-rails and automatic query expansion.