Skip to content
GitHubDiscord

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.


The core of the platform is the LLMClientProtocol. You can switch between 18+ providers (OpenAI, Anthropic, Gemini, etc.) using only configuration.

application.yaml
ai:
llm:
provider: anthropic
model: claude-sonnet-4-6
temperature: 0.7
from lexigram.contracts.ai.llm import LLMClientProtocol
from 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.content

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}")

Agents in Lexigram can use Tools, maintain Memory, and follow orchestrations like ReAct or Plan-and-Execute.

from lexigram.ai.agents import tool
@tool
async def get_stock_price(ticker: str) -> float:
"""Retrieves the real-time stock price for a given ticker symbol."""
return 150.0 # Integration logic here
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)

Lexigram can automatically route requests through multiple providers to balance cost, latency, or availability.

application.yaml
ai:
llm:
routing:
strategy: sequential
providers:
- name: anthropic
model: claude-sonnet-4-6
- name: openai
model: gpt-4o # Fallback if Anthropic fails

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 ThinkingConfig at the provider level.


PackagePurpose
lexigram-ai-llmMulti-provider client & routing.
lexigram-ai-ragDocument ingestion & retrieval.
lexigram-ai-agentsReAct, Planning, and Tool use.
lexigram-ai-memoryShort & long-term AI memory.
lexigram-ai-governanceAI policy and audit logs.
lexigram-ai-guardInput/Output security filters.