DEV Community

shashank ms
shashank ms

Posted on

Building a Reading Comprehension Tool with LLM: Opportunities and Challenges

Reading comprehension was once a narrow NLP benchmark, but large language models have turned it into a general purpose production interface. Modern applications do not simply match questions to paragraphs. They ingest entire whitepapers, legal contracts, or novels, then answer complex questions, cite specific evidence, and synthesize arguments across hundreds of pages. Building these tools is technically straightforward with current APIs, yet moving from prototype to production introduces friction around context windows, inference cost, structured output fidelity, and factual reliability.

What Reading Comprehension Means for LLMs

The academic framing of reading comprehension covers extractive question answering, abstractive summarization, and multi-hop reasoning. In production, the boundary blurs. A user might upload a 200-page regulatory filing and ask, "Which sections mention environmental liability for subcontractors, and how did those obligations change between the 2023 and 2024 versions?" That single query demands long-context ingestion, cross-document comparison, and precise attribution. The LLM must act less like a chatbot and more like a structured analysis engine.

Architectural Patterns

Three patterns dominate production implementations.

Direct long-context prompting is the simplest. You pass the full text and a system instruction to the model. This works well when the source material fits inside the context window and the questions are self-contained.

Chunking with retrieval is the fallback for material that exceeds context limits. The document is split into passages, embedded, and queried via vector search. The top-k chunks are fed into the LLM. This adds infrastructure overhead and can lose global coherence, but it keeps latency low.

Agentic verification loops use function calling to ground claims. The model reads the text, proposes an answer, then calls a tool to verify that a quoted passage actually exists or to retrieve an external definition. This pattern trades latency for accuracy.

All three patterns are supported by the Oxlo.ai chat/completions endpoint. Because the platform is fully OpenAI SDK compatible, you can point an existing client at https://api.oxlo.ai/v1 and run the same code without refactoring.

import openai

client = openai.OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key="YOUR_OXLO_API_KEY"
)

completion = client.chat.completions.create(
    model="deepseek-v4-flash",
    messages=[
        {"role": "system", "content": "Answer only from the provided text. Cite evidence."},
        {"role": "user", "content": f"TEXT:\n{document}\n\nQUESTION: {question}"}
    ]
)

Where Context Length Changes the Economics

For reading comprehension, input length is the primary cost driver. A single legal brief or technical manual can contain tens of thousands of tokens. Under token-based billing, every page you feed into the prompt increases the bill. For agentic workflows that re-read source material across multiple turns, costs compound quickly.

Oxlo.ai uses request-based pricing. You pay one flat cost per API request regardless of prompt length. For long-context and agentic workloads, this can be significantly cheaper than token-based alternatives because cost does not scale with input length. Instead of trimming documents to save tokens, you can pass the full context and let the model reason over it. Oxlo.ai offers models specifically suited for this, including DeepSeek V4 Flash with a 1 million token context window and Kimi K2.6 with a 131K context window and advanced reasoning capabilities. You can compare plans at https://oxlo.ai/pricing.

Handling Hallucination and Attribution

Untrusted answers destroy user confidence in comprehension tools. Two API features help constrain the model: JSON mode and function calling.

JSON mode forces the model to emit valid JSON, which you can schema-check before displaying anything to the user. Function calling lets the model emit a structured request to a verification tool, such as a search index or a quote-checker, rather than hallucinating a citation.

Oxlo.ai supports both features across its LLMs, plus streaming so users see partial results while the model finishes reasoning. The example below combines JSON mode with a tool definition that allows the model to request a secondary lookup if the text is ambiguous.

import openai
import json

client = openai.OpenAI(base_url="https://api.oxlo.ai/v1", api_key="...")

tools = [{
    "type": "function",
    "function": {
        "name": "verify_quote",
        "description": "Confirm the quoted passage exists in the source document",
        "parameters": {
            "type": "object",
            "properties": {
                "quote": {"type": "string"},
                "section": {"type": "string"}
            },
            "required": ["quote"]
        }
    }
}]

response = client.chat.completions.create(
    model="deepseek-r1-671b",
    messages=[
        {"role": "system", "content": "Respond in JSON with keys: answer, evidence_quote, confidence."},
        {"role": "user", "content": f"TEXT: {long_text}\nQUESTION: {question}"}
    ],
    response_format={"type": "json_object"},
    tools=tools,
    tool_choice="auto",
    stream=True
)

for chunk in response:
    if chunk.choices[0].delta.content:
        print(chunk.choices[0].delta.content, end="")

Multi-Turn and Agentic Workflows

Reading is rarely a single-turn interaction. A user asks a question, reads the answer, then asks a follow-up that depends on the previous turn. Maintaining state across turns is trivial with the same conversation array, but agentic comprehension goes further. The model can decide to break a complex question into sub-questions, call a calculator, or query a vector database via function calling.

Because Oxlo.ai has no cold starts on popular models, these loops remain responsive. You are not penalized for keeping a long conversation history in the context window, either, because the platform bills per request rather than per token. That design choice encourages natural, multi-turn interactions instead of forcing developers to aggressively truncate history to cut costs.

Selecting the Right Model

Oxlo.ai hosts more than 45 models across seven categories. For reading comprehension, the choice depends on the text type and the required reasoning depth.

  • General purpose baselines: Llama 3.3 70B is a reliable default for standard Q&A and summarization.
  • Deep reasoning: DeepSeek R1 671B MoE excels at extended chain-of-thought reasoning and complex coding analysis within text.
  • Very long documents: DeepSeek V4 Flash offers a 1 million token context window with efficient MoE architecture.
  • Multilingual corpora: Qwen 3 32B handles agent workflows across languages.
  • Mixed text and visuals: Kimi K2.6 provides advanced reasoning, agentic coding, and vision support within a 131K context window. For image-heavy inputs, you can also use Gemma 3 27B or Kimi VL A3B.
  • Software documentation: Qwen 3 Coder 30B or Oxlo.ai Coder Fast provide code-aware context.

Operational Challenges

Latency is the first operational hurdle. Long-context models take longer to process because attention over hundreds of thousands of tokens is computationally expensive. Streaming responses mitigate this by letting you render tokens as they arrive, which improves perceived performance even if total time is unchanged.

Evaluation is the second hurdle. Reading comprehension systems need automated metrics on held-out passages. Simple exact-match scores work for extractive tasks, but abstractive answers require model-based evaluation or human review. You should maintain a ground-truth dataset that reflects your actual document distribution.

Finally, throughput and queueing matter at scale. Oxlo.ai offers tiered plans that scale daily request allotments and queue priority. The Pro tier provides 1,000 requests per day, while Premium offers 5,000 requests per day with priority queueing. Enterprise plans add dedicated GPUs and unlimited volume. This lets you align infrastructure spend with user demand without managing your own inference cluster.

Putting It Together

Below is a minimal but complete pipeline. It accepts a long document, asks a targeted question, and returns a structured JSON answer with evidence and confidence scoring. The example uses Kimi K2.6 for its balance of reasoning and context length, and it runs against the Oxlo.ai endpoint.

import openai
import json

client = openai.OpenAI(
    base_url="https://api.oxlo.ai/v1",
    api_key="YOUR_OXLO_API_KEY"
)

def analyze_document(document: str, question: str):
    schema = {
        "type": "object",
        "properties": {
            "answer": {"type": "string"},
            "confidence": {"type": "string", "enum": ["high", "medium", "low"]},
            "evidence": {"type": "string"}
        },
        "required": ["answer", "confidence", "evidence"]
    }

    stream = client.chat.completions.create(
        model="kimi-k2-6",
        messages=[
            {"role": "system", "content": f"You are a precise reading assistant. Respond in JSON matching: {json.dumps(schema)}"},
            {"role": "user", "content": f"DOCUMENT:\n{document}\n\nQUESTION: {question}"}
        ],
        response_format={"type": "json_object"},
        stream=True
    )

    for chunk in stream:
        if chunk.choices[0].delta.content:
            print(chunk.choices[0].delta.content, end="")

# Example usage
analyze_document(long_legal_contract, "What termination clauses apply to third-party vendors?")

Conclusion

Building a production reading comprehension tool requires more than a capable LLM. You need context windows large enough to hold source material, API features that constrain output, and a pricing model that does not punish you for using those features. Oxlo.ai addresses each of these requirements. Its request-based pricing removes the penalty for long inputs, its model catalog covers everything from general reasoning to vision and code, and its fully OpenAI-compatible API means integration requires only a base_url change. For teams running long-context or agentic comprehension workloads, Oxlo.ai is a genuinely relevant option worth evaluating. See https://oxlo.ai/pricing for current plan details.

Top comments (0)