Skip to main content

Achieve consensus across multiple LLMs through an AI judge coordinator

Project description

LLM Ensemble

LLM Ensemble Banner

A Python library for achieving consensus across multiple Agents.

Features

  • Consensus: Uses a moderator to iteratively coordinate multiple LLMs until consensus is reached
  • Can use any model supported by Langchain
  • Supports web search for real-time data

Installation

# Clone from GitHub
git clone https://github.com/zzzrbx/llm-ensemble.git
cd llm-ensemble

# Using uv (recommended)
uv sync

# Using pip
pip install -e .

Environment Setup

Create a .env file with your API keys, for example:

OPENAI_API_KEY=your_openai_key
ANTHROPIC_API_KEY=your_anthropic_key
GOOGLE_API_KEY=your_google_key
XAI_API_KEY=your_xai_key
TAVILY_API_KEY=your_tavily_key  # For web search

How It Works

User Query → Judge (configurable, default: Claude Opus 4.5)
    ↓
Judge calls run_llms tool
    ├── Model A (parallel)
    ├── Model B (parallel)
    ├── Model C (parallel)
    └── Model D (parallel)
         ↓
Judge analyzes responses
    ├── Consensus? → Return answer
    └── No consensus? → Refine query and call run_llms again
         ↓
Repeat until consensus or limit reached

Key Features:

  • Judge is unbiased - determines consensus based only on LLM responses, not its own knowledge
  • Dynamic queries - Judge crafts different prompts each iteration:
    • Iteration 1: Sends initial question with research instructions
    • Iteration 2+: Summarizes agreements, highlights disagreements, requests refinements
    • Final iteration: Presents refined consensus statement for confirmation
  • Error handling - Returns default values on timeout or tool call limit reached

Tools currently available for LLMs:

  • search_the_web - Tavily web search for current events and factual data
  • add, subtract, multiply, divide - Math operations

Examples

Example 1: With structured output

from typing import TypedDict
from llm_ensemble import Consensus

class UserSchema(TypedDict):
    consensus: bool
    final_answer: str
    notes: str

consensus = Consensus(
    models=[
        "openai:gpt-5-mini",
        "google_genai:gemini-3-flash-preview",
        "anthropic:claude-3-5-haiku-20241022",
        "xai:grok-3-mini",
    ],
    response_schema=UserSchema
)

result = consensus.invoke(
    "If survival is arbitrary, is moral judgment arbitrary too?"
)

print(f"Consensus: {result['consensus']}")
print(f"Answer: {result['final_answer']}")
print(f"Notes: {result['notes']}")

Example 2: No structured output with web search

from llm_ensemble import Consensus

# No response_schema - returns full agent result
consensus = Consensus(
    models=[
        "openai:gpt-5-mini",
        "google_genai:gemini-3-flash-preview",
        "anthropic:claude-3-5-haiku-20241022",
        "xai:grok-3-mini",
    ]
)

result = consensus.invoke(
    "What are the latest developments in quantum computing?\n\n"
    "Use the web search to research current news and breakthroughs."
)

# Access full agent result
print(result['messages'][-1].content)

Debugging and Observability

The library integrates with LangSmith for trace observability. Set LANGSMITH_API_KEY and LANGSMITH_PROJECT in your .env file to enable tracing.

License

MIT License

Contributing

Contributions are welcome! Please open an issue or submit a pull request.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

multi_llm_consensus-0.1.0.tar.gz (534.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

multi_llm_consensus-0.1.0-py3-none-any.whl (11.8 kB view details)

Uploaded Python 3

File details

Details for the file multi_llm_consensus-0.1.0.tar.gz.

File metadata

  • Download URL: multi_llm_consensus-0.1.0.tar.gz
  • Upload date:
  • Size: 534.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.0

File hashes

Hashes for multi_llm_consensus-0.1.0.tar.gz
Algorithm Hash digest
SHA256 baabb09336e7b6afa05f030498429ad5aafec741ce8c36f95a3b9275596bb0f5
MD5 f30282284300748f5768086ed4ec9f7f
BLAKE2b-256 e7a9c04d2bd47345bbbf33015b8b1e298c5d0f022f0dbfe3b347647ee893c965

See more details on using hashes here.

File details

Details for the file multi_llm_consensus-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for multi_llm_consensus-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2d4ac32029e0e301e24e6757ab0b9fc403211830f3ffe0cb57477cfa6c5e125d
MD5 774c7f5edf772d827462f106eac141ab
BLAKE2b-256 5b2cdfb2b8207c571d46c2d65adc2b19bf1ee6b1b204cea32311023d04f6a43e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page