AI Integration
Lexigram provides a comprehensive suite of AI primitives designed for security, observability, and modularity. The lexigram-ai platform handles everything from multi-provider LLM orchestration to complex RAG pipelines and autonomous agents.
1. Minimal LLM Setup
Section titled “1. Minimal LLM Setup”The core of the platform is the LLMClientProtocol. You can switch between 18+ providers (OpenAI, Anthropic, Gemini, etc.) using only configuration.
Configuration
Section titled “Configuration”ai: llm: provider: anthropic model: claude-sonnet-4-6 temperature: 0.7from lexigram.contracts.ai.llm import LLMClientProtocolfrom lexigram.contracts.ai.types import ChatMessage, Role
class ChatService: def __init__(self, llm: LLMClientProtocol) -> None: self.llm = llm
async def get_response(self, prompt: str) -> str: response = await self.llm.complete( messages=[ChatMessage(role=Role.USER, content=prompt)] ) return response.content2. RAG Pipelines (Retrieval Augmented Generation)
Section titled “2. RAG Pipelines (Retrieval Augmented Generation)”Lexigram RAG coordinates document loading, embedding, vector storage, and retrieval.
from lexigram.ai.rag import RAGPipelineProtocol
async def ask_documents(rag: RAGPipelineProtocol, query: str): # This single call metadata retrieval, context synthesis, # and LLM generation. result = await rag.query(query)
print(f"Answer: {result.answer}") print(f"Sources: {result.sources}")3. Autonomous Agents
Section titled “3. Autonomous Agents”Agents in Lexigram can use Tools, maintain Memory, and follow orchestrations like ReAct or Plan-and-Execute.
Defining a Tool
Section titled “Defining a Tool”from lexigram.ai.agents import tool
@toolasync def get_stock_price(ticker: str) -> float: """Retrieves the real-time stock price for a given ticker symbol.""" return 150.0 # Integration logic hereExecuting an Agent
Section titled “Executing an Agent”from lexigram.ai.agents import AgentExecutorProtocol
async def run_investor_agent(executor: AgentExecutorProtocol): response = await executor.run( "Should I buy AAPL? Check its current price first.", tools=["get_stock_price"] ) print(response.output)4. Multi-Provider Routing & Resilience
Section titled “4. Multi-Provider Routing & Resilience”Lexigram can automatically route requests through multiple providers to balance cost, latency, or availability.
ai: llm: routing: strategy: sequential providers: - name: anthropic model: claude-sonnet-4-6 - name: openai model: gpt-4o # Fallback if Anthropic fails5. Governance & Observability
Section titled “5. Governance & Observability”AI workflows are notoriously opaque. Lexigram provides built-in tools to monitor and control them.
- Tracing: Full visibility into agent reasoning loops and RAG retrieval steps.
- Guards: Input/Output filtering to prevent prompt injection or PII leakage.
- Cost Tracking: Real-time token usage metrics and cost estimation per model.
[!TIP] Use Thinking Suppression for models like Claude Sonnet or OpenAI o1 to hide internal reasoning tokens while still benefiting from their advanced logic. This is configured in
ThinkingConfigat the provider level.
Technical Stack
Section titled “Technical Stack”| Package | Purpose |
|---|---|
lexigram-ai-llm | Multi-provider client & routing. |
lexigram-ai-rag | Document ingestion & retrieval. |
lexigram-ai-agents | ReAct, Planning, and Tool use. |
lexigram-ai-memory | Short & long-term AI memory. |
lexigram-ai-governance | AI policy and audit logs. |
lexigram-ai-guard | Input/Output security filters. |