Skip to main content

The Confidence Game: How AI Learns to Know When It Doesn't Know 🎲

Or: Why my legal AI sometimes calls for backup (and sometimes just goes for it)

Published 3 min readMarkdown
Part of seriesSmart Legal QA — Building an AI Legal Assistant with RAG & LangChain
The Confidence Game: How AI Learns to Know When It Doesn't Know 🎲

Chapter 1: The AI’s Moment of Truth 🤔

Imagine our legal AI assistant, fresh from a victorious reranking championship, standing before a pile of sorted documents. The user’s question echoes: “What does Section 302 say?” The AI glances at its stack and wonders… “Am I sure about this?”

This is where the confidence game begins.

Chapter 2: The Confidence Calculator Arrives 🧮

Our AI doesn’t just guess. It calculates! Enter the confidence calculation function, the referee of our system:

def calculate_confidence(docs, query_analysis):
    base_score = 0.6

    # Section match boost
    if query_analysis['sections']:
        section_matches = count_section_matches_in_docs(docs)
        base_score += (section_matches / len(docs)) * 0.2

    # Act match boost
    if query_analysis['acts']:
        act_matches = count_act_matches_in_docs(docs)
        base_score += (act_matches / len(docs)) * 0.15

    # Content length penalty
    avg_content_length = calculate_avg_content_length(docs)
    if avg_content_length < 200:
        base_score -= 0.1

    return min(base_score, 0.95)

What’s happening here?

  • The AI starts with a base confidence (0.6).

  • If the retrieved docs match the requested section or act, it gets a boost.

  • If the docs are too short, it gets a penalty.

  • The final confidence is capped at 0.95.

Chapter 3: The Fork in the Road 🛣️

Now, the AI faces a decision. Is it confident enough to answer directly, or does it need to call for backup?

Here’s the pivotal moment in code:

if confidence < 0.3:  # Only use tools for very low confidence
    answer, tools_used = await execute_tools_intelligently(
        query=payload.question,
        context=context,
        complexity_level=payload.complexity_level,
        llm=llm,
        confidence=confidence
    )
else:
    # Use direct LLM with context
    prompt = ENHANCED_LEGAL_PROMPT.format(
        complexity_level=payload.complexity_level,
        query_analysis=query_analysis,
        context=context
    )
    response = llm.invoke([
        {"role": "system", "content": prompt},
        {"role": "user", "content": payload.question}
    ])
    answer = response.content
    tools_used = []

If confidence is high (≥ 0.3):

  • The AI trusts its retrieval and lets the LLM answer using the context.

  • No extra tools, no extra fuss.

If confidence is low (< 0.3):

  • The AI gets nervous and calls for backup.

  • It runs special tools (like citation fetchers or section explainers) to gather more info before answering.

Chapter 4: Why Play the Confidence Game? 🎲

This isn’t just for drama. By calculating confidence and branching its strategy, the AI:

  • Avoids giving weak or misleading answers when it’s unsure.

  • Uses extra resources only when necessary.

  • Makes the system more reliable for tricky legal queries.

Chapter 5: The Wisdom of Knowing When to Ask for Help 🧠

So, next time you ask a tough legal question, remember: behind the scenes, your AI is playing the confidence game. Sometimes it’s bold, sometimes it’s cautious, but it always knows when to call for backup.

And that’s how our legal AI learns to know when it doesn’t know.


Stay tuned for the next chapter, where we’ll explore how those backup tools work their magic!

No comments yet. Be the first to comment.

Smart Legal QA — Building an AI Legal Assistant with RAG & LangChain

Part 6 of 9

Documenting how I designed and built an AI assistant that can answer complex legal questions from uploaded PDFs. Learn about RAG pipelines, LangChain, embeddings, evaluation datasets, and architectural choices.

Up nextCalling for Backup: How AI Tools Step In When Confidence Is Low 🛠️Or: What happens when my legal AI gets nervous and calls its friends for help