A sophisticated multi-agent system for constitutional law research using Google's Gemini 2.5 Pro with custom tool calling and SQLite database integration.
This system consists of four specialized agents working together to provide comprehensive constitutional law research:
- UI Agent - Processes natural language user input and converts it to structured JSON queries
- Main Agent - Orchestrates the overall workflow and coordinates between agents
- Research Agent - Collects extensive legal data from multiple sources (cases, statutes, articles)
- Documentation Agent - Generates professional, structured documentation suitable for legal use
User Query → UI Agent → Main Agent → Research Agent → Documentation Agent → Final Report
↓ ↓ ↓ ↓
SQLite DB Status Mgmt Legal APIs Structured JSON
- Natural Language Processing: Convert plain English queries into structured legal research parameters
- Comprehensive Research: Searches case law, statutes, pending cases, and scholarly articles
- Professional Documentation: Generates executive summaries, legal analysis, and recommendations
- Database Integration: Persistent storage of all queries, research, and results
- Explainability & Traceability: Structured logs, artefact snapshots, and decision metadata for every agent
- Deterministic Controls: Pinned prompts, zero-temperature LLM settings, and normalized external queries ensure repeatable outcomes
- Retry Logic: Robust error handling with exponential backoff
- Status Tracking: Real-time status updates for long-running research tasks
- Python 3.8+
- Google Gemini API key
- SQLite (included with Python)
-
Clone or download the system files
-
Install dependencies:
pip install google-generativeai sqlite3 asyncio
-
Set up API keys:
Option A: Environment Variables (Recommended)
export GEMINI_API_KEY="your-gemini-api-key-here"
Option B: Edit config.py
GEMINI_API_KEY = "your-actual-gemini-api-key-here"
-
Initialize the database: The database will be created automatically on first run.
python main.pyThis starts an interactive session where you can:
- Enter constitutional law research queries
- Check status of ongoing research
- View completed results
- Get help and guidance
python main.py "What are the current limits on executive power?"- "What is the current status of affirmative action in education?"
- "How has the Equal Protection Clause been interpreted in recent Supreme Court cases?"
- "What are the constitutional limits on presidential emergency powers?"
- "How do state religious freedom laws interact with federal civil rights protections?"
help- Show available commandsstatus <request_id>- Check status of a research requestresult <request_id>- Display full research resultsclear- Clear the screenquit- Exit the program
id- Primary keyuser_id- User identifiertimestamp- When request was submittedoriginal_query- Original user questionquery_summary- Structured JSON of research parametersstatus- Current status (pending, researching, documenting, completed)
id- Primary keyrequest_id- Foreign key to user_requestssources- JSON array of research sourcescase_laws- JSON array of relevant casesstatutes- JSON array of constitutional provisionspending_cases- JSON array of ongoing casesarticles- JSON array of scholarly articlesresearch_timestamp- When research was completed
id- Primary keyrequest_id- Foreign key to user_requestsoutput_json- Complete structured documentationcreation_timestamp- When documentation was generated
The system includes placeholders for integration with legal databases:
- CourtListener API - Federal court opinions
- Justia API - Legal resources
- Qdrant Vector Database - RAG-based document search
To implement these integrations:
- Sign up for API access with the respective services
- Add your API keys to
config.py - Implement the actual API calls in
research_agent.py
Modify agent prompts in the respective agent files to customize:
- Research focus areas
- Documentation style
- Analysis depth
- Output format
Extend the database schema in database.py to add:
- User management
- Research categories
- Citation tracking
- Usage analytics
-
User Input Processing
- UI Agent receives natural language query
- Gemini converts to structured JSON
- Query stored in database
-
Research Coordination
- Main Agent updates status to "researching"
- Research Agent searches multiple sources
- Results deduplicated and validated
- Research data stored in database
-
Documentation Generation
- Main Agent updates status to "documenting"
- Documentation Agent processes research results
- Gemini generates structured legal analysis
- Final documentation stored and marked complete
The system minimizes randomness so retries give consistent results:
- Prompt Registry: Agent prompts are stored as versioned templates (see
ui_agent.py,research_agent.py,documentation_agent.py) so text generation always starts from the same instructions. - LLM Settings: The Gemini client uses temperature
0,top_p0, and capped token budgets viaconfig.py, eliminating sampling variance. - Retry Strategy:
MainAgentdrives exponential backoff with fixed intervals, and failed runs log the exact attempt count. - Query Normalization:
ResearchAgentcanonicalizes Indian Kanoon queries (ordering, spacing, removal of redundant tokens) before hitting external APIs. - Shared State:
TraceLoggerhashes payloads; comparing hashes across runs quickly shows whether responses changed.
Together these controls make it straightforward to reproduce outputs across environments or identify when an upstream dependency diverges.
Every agent interaction now leaves an audit trail so you can replay exactly how a report was built:
- Structured Logs (
trace_logstable) capture who did what, when, and why with hashed payloads. - Artefact Snapshots (
artefact_snapshotstable) preserve the raw Gemini responses alongside the cleaned, structured objects stored in the system. - Decision Metadata (
decision_metadatatable) records each agent's reasoning (for example, how queries were structured or why a tool plan was chosen).
All three tables link back to the originating request_id, making it easy to filter for a specific run.
.\.venv\Scripts\python.exe -c "import sqlite3, json; conn = sqlite3.connect('constitutional_law.db'); conn.row_factory = sqlite3.Row; cur = conn.cursor();
for table in ('trace_logs','artefact_snapshots','decision_metadata'):
cur.execute('SELECT * FROM ' + table + ' WHERE request_id=? ORDER BY id', (13,));
rows = [dict(row) for row in cur.fetchall()];
print(f'== {table} ==');
if not rows:
print('(no rows)');
else:
for row in rows:
print(json.dumps(row, indent=2, default=str));
conn.close()"Replace 13 with any other request ID you want to audit. The output includes the timestamped payloads, Gemini artefacts, and decision rationales for that run.
- Custom Exception Hierarchy: Specific exceptions for different failure modes
- Retry Logic: Exponential backoff for API rate limits
- Graceful Degradation: Fallback responses when AI generation fails
- Database Rollback: Consistent state even during failures
- Store API keys in environment variables
- Implement user authentication
- Add input sanitization
- Use HTTPS for API communication
- Add connection pooling for database
- Implement async/await throughout
- Add caching layer for repeated queries
- Consider microservices architecture
- Add structured logging
- Implement metrics collection
- Monitor API usage and costs
- Track research quality metrics
This system is for research and educational purposes. Always verify legal information with qualified attorneys. The system provides research assistance but does not constitute legal advice.
"API key not found"
- Ensure GEMINI_API_KEY is set in environment or config.py
"Database locked"
- Close any other processes accessing the database file
- Check file permissions
"Research timeout"
- Increase timeout values in config.py
- Check internet connection for API access
"JSON parsing error"
- This typically resolves with retry logic
- Check Gemini API status if persistent
Check the console output for detailed error messages and processing status.
To extend this system:
- Add new data sources in
research_agent.py - Enhance documentation formats in
documentation_agent.py - Improve query processing in
ui_agent.py - Add new agent types following the existing pattern
This system is provided as-is for educational and research purposes.