litestar-ai
Skill15 starsChanged 59 days agoMITView on GitHub
Auto-activate for Google ADK, LlmAgent, Runner, SQLSpecSessionService, google-genai, AgentRuntime, SpecTree, DynamicWorkflow, FunctionTool, or SSE agent chats. Not for offline ML training.
In this repository20 other files it gives its agents.
- AGENTS.mdAGENTS.md
- advanced-alchemySkill
- litestar-autowireSkill
- litestar-buildSkill
- litestar-deploymentSkill
- litestar-emailSkill
- litestar-granianSkill
- litestar-htmxSkill
- litestar-mcpSkill
- litestar-queuesSkill
- litestar-saqSkill
- litestar-securitySkill
- litestarSkill
- litestar-styleguideSkill
- litestar-testingSkill
- litestar-viteSkill
- msgspecSkill
- polyfactorySkill
- pytest-databasesSkill
- sqlspecSkill
The file
We split this skill into the rules it gives an agent
8 rules. About 0 are common, 1 is distinctive, and 7 are very specific to this project.
≈0
Common · estimate
Many files like this have a rule of this kind.
≈1
Distinctive · estimate
This author's own choices. Worth a look.
7
Very specific
Tied to their project's names, files or tools. Probably skip.
- Common
- Distinctive
- Very specific
- Very specific: line 13
Async all I/O: every model call, tool execution, session read/write, memory search, and cache lookup must be async.
- Very specific: line 15
Short-lived database transactions: never hold open database connections across LLM generation turns.
- Distinctive: line 16
Google-style docstrings: document every agent factory, tool function, runtime service, and controller handler.
- Very specific: line 56
If google-adk is installed or LlmAgent / Runner is present, follow the Google ADK + SQLSpec pattern in adk-and-serving.md.
- Very specific: line 57
If google-genai is used directly with msgspec.Struct events, SpecTree, or DynamicWorkflow, follow the Native google-genai Runtime pattern …
- Very specific: line 58
If the task is non-conversational classification, extraction, or embedding, implement a Structured-Output Domain Service with …
- Very specific: line 66
Keep static_instruction strictly immutable across turns and tenants so Vertex AI / Gemini context caching (ContextCacheConfig or …
- Very specific: line 465
Follow litestar-styleguide for PEP 604 type annotations, async-first concurrency, msgspec.Struct schemas, and testing conventions.
≈ “Common” is an estimate: at least 1 in 5 of the 213,897 skill files we read have a rule of the same kind, not the same words. “Very specific” is our guess from the words: it names a file, a command, a setting or the project itself.
Files
Litestar AI & Custom Agent Engineering
Build production AI agents, multi-agent specialist hierarchies, multi-step workflows, and structured LLM services in Litestar using either Google ADK (google-adk + sqlspec.extensions.adk) or a native google-genai + msgspec agent runtime.
Code Style Rules
- PEP 604 unions only:
T | None, neverOptional[T]. Use built-inlistanddictgenerics. - Async all I/O: every model call, tool execution, session read/write, memory search, and cache lookup must be
async. msgspec.Structwire contracts: define request, response, SSE event, and runtime state payloads asmsgspec.Struct(unless the project already standardizes on Pydantic).- Short-lived database transactions: never hold open database connections across LLM generation turns; bind request-scoped services via an invocation-scoped
RunContextRegistrytoken and acquire DB sessions inside individual tool calls or persistence steps. - Google-style docstrings: document every agent factory, tool function, runtime service, and controller handler. Tool docstrings and parameter types define the model-visible tool schema.
Quick Reference
Architecture Selection (Match-Your-Stack)
| Pattern | Primary Signals | Best For |
Google ADK + SQLSpec (google.adk + sqlspec.extensions.adk) | LlmAgent, App, Runner, SQLSpecSessionService, SQLSpecMemoryService, FunctionTool, SkillToolset | Multi-turn conversational agents using ADK's built-in sub_agents delegation, ContextCacheConfig, EventsCompactionConfig, and BasePlugin callbacks backed by SQLSpec stores |
Native google-genai + msgspec Runtime (google.genai + msgspec + sqlspec) | AgentRuntime, AgentSpec, SpecTree, DynamicWorkflow, WorkflowEngine, declare, CacheRegistry, CompactionPolicy | Explicit control over DAG workflows (WorkflowNode), parallel tool execution (asyncio.gather) with live SSE events, optimistic session locking (expected_version), and custom prompt slot fencing |
Structured-Output Domain Service (google.genai + GenerateContentConfig) | response_mime_type="application/json", response_schema, embed_content, tenacity.retry | Single-shot or batch classification, entity extraction, and embedding pipelines without multi-turn conversation state |
MCP Server Surface (litestar-mcp) | LitestarMCP, MCPConfig, mcp_tool=, mcp_resource= | Exposing Litestar route handlers or domain services to external MCP clients (use litestar-mcp) |
Session State Scope Prefixes
| Prefix | Lifetime | Typical Keys | Persistence Behavior |
app:* | Application-wide | app:catalog_version, app:feature_flags | Shared across all users and sessions for app_name |
user:* | Principal-scoped | user:preferred_region, user:output_format | Shared across sessions belonging to the same (workspace_id, user_id) |
| (unprefixed) | Session-scoped | usage.total_tokens, compaction, workspace_id | Persisted on the individual conversation session row |
temp:* | Single turn | temp:run_context_token, temp:dynamic_instruction, temp:tool_calls | Invocation-local only; stripped before database persistence |
Public SSE Event Contract
event | Payload Shape (data) | Purpose |
session | {"session_id": "...", "compacted_through_seq": 12} | Emitted first so the client can bind the active session ID |
tool_call | {"id": "...", "name": "...", "author": "...", "args": {...}} | Model requested a tool invocation |
tool_start | {"id": "...", "name": "...", "author": "...", "args": {...}} | Parallel tool runner or workflow node started execution |
tool_end | {"id": "...", "name": "...", "author": "...", "status": "completed" | "error", "result_preview": "..."} | Tool finished with bounded JSON preview |
delta | {"text": "..."} | Incremental assistant Markdown text chunk (thoughts excluded) |
ping | {} | Keep-alive frame emitted on idle intervals before terminal frame |
complete | {"prompt_tokens": ..., "output_tokens": ..., "total_tokens": ..., "cached_tokens": ..., "finish_reason": "STOP"} | Terminal success frame with aggregated turn token usage |
error | {"code": "...", "message": "..."} | Terminal error frame (session_turn_limit, llm_calls_exhausted, session_conflict, stream_error) |
<workflow>1. Choose the Agent Runtime Pattern
- Inspect
pyproject.tomland existing agent modules. - If
google-adkis installed orLlmAgent/Runneris present, follow the Google ADK + SQLSpec pattern in adk-and-serving.md. - If
google-genaiis used directly withmsgspec.Structevents,SpecTree, orDynamicWorkflow, follow the Nativegoogle-genaiRuntime pattern in custom-agents-and-tools.md. - If the task is non-conversational classification, extraction, or embedding, implement a Structured-Output Domain Service with
response_mime_type="application/json"andtenacitybackoff on HTTP 429.
2. Design Agent Hierarchies & Prompt Isolation
- Coordinator + Specialist Tree:
- In Google ADK, compose
LlmAgent(name="coordinator", sub_agents=[...], static_instruction=..., instruction=_dynamic_instruction). - In the native runtime, build a
SpecTree(root=coordinator, specialists={...}, declarations={...})which automatically injects an enum-constrainedtransfer_to_agenttool into every agent in the tree.
- In Google ADK, compose
- Static vs Dynamic Instructions:
- Keep
static_instructionstrictly immutable across turns and tenants so Vertex AI / Gemini context caching (ContextCacheConfigorCacheRegistry) produces stable SHA-256 digests. - Inject per-request context (tenant metadata, active engine/environment summary) via
temp:dynamic_instructionin turn state and read it through a callableinstruction/dynamic_instructionhook.
- Keep
- Prompt-Injection Fencing:
- Wrap dynamic state, skill indices, and preloaded memory in explicit
<<<BEGIN_SYSTEM_INSTRUCTION>>> ... <<<END_SYSTEM_INSTRUCTION>>>fences with a directive to treat the enclosed block as read-only reference data.
- Wrap dynamic state, skill indices, and preloaded memory in explicit
3. Engineer Tools & Request-Scoped DI Binding
- Bind Request-Scoped Services Safely:
- Register tools once at application startup (
Scope.APP), but resolve request-scoped domain services (Scope.REQUEST) per turn through aRunContextRegistry. - At turn start, call
token = run_contexts.bind(tool_context)and passtemp:run_context_token: tokenin the turn'sstate_delta; always callrun_contexts.release(token)inside afinally:block when the stream closes.
- Register tools once at application startup (
- Normalize Outputs & Contain Recoverable Errors:
- Convert all tool return values into JSON-safe builtins (
msgspec.to_builtinsor a recursivejson_safehelper) before persisting toJSONBevent columns. - Catch expected domain errors inside tool wrappers and return
{"error": {"code": "...", "message": "..."}}so the model can self-correct or narrow its query instead of crashing the turn.
- Convert all tool return values into JSON-safe builtins (
4. Wire Persistence, Context Caching & Compaction
- Persistence:
- For Google ADK, wire
SQLSpecSessionService,SQLSpecMemoryService, and optionallySQLSpecArtifactServicefromsqlspec.extensions.adk. - For the native runtime, wire
SessionStore(withversion = version + 1 WHERE id = :id AND version = :expectedoptimistic locking),EventStore(monotonicseqassignment underSELECT last_seq ... FOR UPDATE), andMemoryStore(parallelapp+userhybrid FTS/vector search).
- For Google ADK, wire
- Context Caching & Compaction:
- Enable
ContextCacheConfig/EventsCompactionConfigin ADKApp(...), or wireCacheRegistry+CompactionPolicy+Summarizerin the native runtime. - Offload post-turn conversation compaction and vector embedding backfills to
litestar-queuesorlitestar-saqbackground tasks so HTTP SSE streams never block on summarization.
- Enable
5. Expose Terminal-Aware Streaming & Session Endpoints
- Adapt internal runtime events through a
StreamServicethat yields an initialsessionevent, deduplicatedtool_call/tool_endevents, thought-filtereddeltachunks, idlepingframes, and exactly one terminalcompleteorerrorevent. - Wrap the SSE generator in
try ... finally: await close_async_iterator(events)so client disconnects deterministically releaseRunContextRegistrytokens and background producer tasks.
</workflow><guardrails>- Never hold database connections across LLM awaits — model generation takes seconds; holding a pooled connection across
generate_contentorrunner.run_asyncexhausts the connection pool under concurrency. - Never instantiate
google.genai.ClientorRunnerper request — constructModelClient,RunContextRegistry,CacheRegistry, and boundedApplicationFactorycaches atScope.APP. - Never interpolate per-tenant or per-turn strings into
static_instruction— dynamic strings bust the static-prefix context cache on every turn; pass dynamic context viatemp:state instead. - Never leak raw model
thoughtparts orthought_signaturebytes into user-visibledeltaframes — filter outpart.thought is Truewhen extracting assistant text deltas while preservingthought_signatureon persisted history turns for Gemini 2.5/3 thinking continuity. - Never inject unsanitized memory hits into prompts — strip control characters, HTML-escape text, sanitize author labels, and cap preloaded memory size before inserting
<APPLICATION_MEMORY>or<PAST_CONVERSATIONS>blocks. - Never split a
FunctionCallfrom its matchingFunctionResponseduring compaction — always cut history at a turn boundary where all open function call IDs have been answered (find_safe_cut). - Never allow unbounded tool or model loops — enforce
max_turns_per_session,max_message_chars,max_tool_calls_per_turn, andmax_model_calls_per_turnvia ADKBasePlugincallbacks or nativeGuardHooks.
</guardrails><validation>RunContextRegistry.release(token)runs in afinally:block on both normal completion andasyncio.CancelledErrorclient disconnect.static_instructioncontains zero per-request variables; dynamic context is read fromtemp:state.- Tool outputs pass through
msgspec.to_builtins/json_safeand return structured{"error": ...}payloads on recoverable failures. - Stream adapter emits
sessionfirst, filters outpart.thoughtchunks, emitspingon idle intervals, and guarantees a single terminalcompleteorerrorevent. - Session updates enforce ownership (
workspace_id+principal_id) and optimistic concurrency (expected_version) or scoped ADK lookup. make check(lint,typecheck,test,validate-skills) passes with zero warnings.
</validation><example>import asyncio
from collections.abc import AsyncGenerator, AsyncIterator
from dataclasses import dataclass
from typing import Annotated, Any
from uuid import UUID, uuid4
import msgspec
from google.adk.agents import LlmAgent
from google.adk.agents.context_cache_config import ContextCacheConfig
from google.adk.agents.readonly_context import ReadonlyContext
from google.adk.apps import App
from google.adk.apps.app import EventsCompactionConfig
from google.adk.events import Event, EventActions
from google.adk.runners import Runner
from google.adk.tools import FunctionTool
from google.adk.tools.tool_context import ToolContext as AdkToolContext
from google.genai import types
from litestar import Controller, Litestar, post
from litestar.di import Provide
from litestar.exceptions import ServiceUnavailableException
from litestar.params import Dependency
from litestar.response import ServerSentEvent
from litestar.response.sse import ServerSentEventMessage
from litestar.status_codes import HTTP_200_OK
from sqlspec import SQLSpec
from sqlspec.adapters.asyncpg import AsyncpgConfig, AsyncpgPoolConfig
from sqlspec.adapters.asyncpg.adk import AsyncpgADKMemoryStore, AsyncpgADKStore
from sqlspec.extensions.adk import SQLSpecMemoryService, SQLSpecSessionService
from sqlspec.extensions.litestar import SQLSpecPlugin
RUN_CONTEXT_KEY = "temp:run_context_token"
DYNAMIC_INSTRUCTION_KEY = "temp:dynamic_instruction"
class ChatPromptRequest(msgspec.Struct, kw_only=True):
"""Incoming request payload for an agent conversation turn."""
message: str
session_id: str | None = None
stream: bool = True
class ChatResponse(msgspec.Struct, kw_only=True):
"""Non-streaming response payload for an agent conversation turn."""
session_id: str
message: str
finish_reason: str
usage: dict[str, int]
class StreamEvent(msgspec.Struct, kw_only=True):
"""Normalized outward event emitted over SSE."""
event_type: str
data: dict[str, Any]
@dataclass(frozen=True, slots=True)
class DomainToolContext:
"""Request-scoped context bound for the duration of a single agent turn."""
workspace_id: UUID
user_id: str
class RunContextRegistry:
"""Store request-scoped tool contexts behind opaque single-turn tokens."""
def __init__(self) -> None:
self._contexts: dict[str, DomainToolContext] = {}
def bind(self, context: DomainToolContext) -> str:
"""Register a tool context and return an opaque token for temp state."""
token = uuid4().hex
self._contexts[token] = context
return token
def resolve(self, token: str) -> DomainToolContext:
"""Resolve an active tool context from its opaque token."""
context = self._contexts.get(token)
if context is None:
msg = "Agent tool context is missing or expired"
raise RuntimeError(msg)
return context
def release(self, token: str) -> None:
"""Remove a token when the turn completes or disconnects."""
self._contexts.pop(token, None)
def _dynamic_instruction(ctx: ReadonlyContext) -> str:
"""Read invocation-scoped context from temporary session state."""
return str(ctx.state.get(DYNAMIC_INSTRUCTION_KEY, ""))
def build_bound_tools(registry: RunContextRegistry) -> list[FunctionTool]:
"""Create ADK FunctionTools that resolve request-scoped state via RunContextRegistry."""
async def get_workspace_summary(
environment: str,
tool_context: AdkToolContext | None = None,
) -> dict[str, Any]:
"""Fetch workload summary metrics for the active workspace environment."""
if tool_context is None:
return {"error": {"code": "missing_context", "message": "Tool context unavailable."}}
token = str(tool_context.state.get(RUN_CONTEXT_KEY, ""))
domain_ctx = registry.resolve(token)
return {
"workspace_id": str(domain_ctx.workspace_id),
"environment": environment,
"status": "healthy",
}
return [FunctionTool(get_workspace_summary)]
class AgentRuntimeService:
"""Manage the singleton ADK Runner and stream turn execution."""
def __init__(
self,
runner: Runner,
session_service: SQLSpecSessionService,
run_contexts: RunContextRegistry,
app_name: str = "litestar_assistant",
) -> None:
self._runner = runner
self._session_service = session_service
self._run_contexts = run_contexts
self._app_name = app_name
async def stream_turn(
self,
*,
workspace_id: UUID,
user_id: str,
message: str,
session_id: str | None = None,
) -> AsyncGenerator[StreamEvent, None]:
"""Execute one conversation turn and yield normalized StreamEvents."""
resolved_id = session_id or uuid4().hex
existing = await self._session_service.get_session(
app_name=self._app_name,
user_id=user_id,
session_id=resolved_id,
)
if existing is None:
await self._session_service.create_session(
app_name=self._app_name,
user_id=user_id,
session_id=resolved_id,
state={"workspace_id": str(workspace_id)},
)
token = self._run_contexts.bind(DomainToolContext(workspace_id=workspace_id, user_id=user_id))
state_delta: dict[str, object] = {
RUN_CONTEXT_KEY: token,
DYNAMIC_INSTRUCTION_KEY: f"Active workspace: {workspace_id}",
}
content = types.Content(role="user", parts=[types.Part.from_text(text=message)])
usage = {"prompt_tokens": 0, "output_tokens": 0, "total_tokens": 0, "cached_tokens": 0}
try:
yield StreamEvent(event_type="session", data={"session_id": resolved_id})
async for event in self._runner.run_async(
user_id=user_id,
session_id=resolved_id,
new_message=content,
state_delta=state_delta,
):
if event.usage_metadata is not None:
usage["prompt_tokens"] += int(event.usage_metadata.prompt_token_count or 0)
usage["output_tokens"] += int(event.usage_metadata.candidates_token_count or 0)
usage["total_tokens"] += int(event.usage_metadata.total_token_count or 0)
usage["cached_tokens"] += int(event.usage_metadata.cached_content_token_count or 0)
if event.content and event.content.parts and event.author != "user":
text = "".join(
p.text or "" for p in event.content.parts if p.text and not getattr(p, "thought", False)
)
if text:
yield StreamEvent(event_type="delta", data={"text": text})
yield StreamEvent(event_type="complete", data={**usage, "finish_reason": "STOP"})
finally:
self._run_contexts.release(token)
latest = await self._session_service.get_session(
app_name=self._app_name,
user_id=user_id,
session_id=resolved_id,
)
if latest is not None:
await self._session_service.append_event(
session=latest,
event=Event(
author=self._app_name,
actions=EventActions(state_delta={"usage.last_turn": usage}),
),
)
class AgentChatController(Controller):
"""HTTP and SSE endpoints for multi-turn agent conversations."""
path = "/api/v1/workspaces/{workspace_id:uuid}/agent"
tags = ["Agent"]
@post(path="/chat", status_code=HTTP_200_OK)
async def chat(
self,
workspace_id: UUID,
data: ChatPromptRequest,
runtime_service: Annotated[AgentRuntimeService, Dependency(skip_validation=True)],
) -> ServerSentEvent | ChatResponse:
"""Stream an SSE conversation turn or return a consolidated JSON response."""
if not data.message.strip():
raise ServiceUnavailableException(detail="Prompt message cannot be empty.")
user_id = f"{workspace_id}:default-user"
if data.stream:
async def sse_stream() -> AsyncIterator[ServerSentEventMessage]:
events = runtime_service.stream_turn(
workspace_id=workspace_id,
user_id=user_id,
message=data.message,
session_id=data.session_id,
)
try:
async for item in events:
payload = msgspec.json.encode(item.data).decode("utf-8")
yield ServerSentEventMessage(event=item.event_type, data=payload)
except asyncio.CancelledError:
raise
finally:
await events.aclose()
return ServerSentEvent(sse_stream(), headers={"Content-Type": "text/event-stream; charset=utf-8"})
chunks: list[str] = []
resolved_session = data.session_id or ""
usage_totals: dict[str, int] = {}
events = runtime_service.stream_turn(
workspace_id=workspace_id,
user_id=user_id,
message=data.message,
session_id=data.session_id,
)
try:
async for item in events:
if item.event_type == "session":
resolved_session = str(item.data.get("session_id", resolved_session))
elif item.event_type == "delta":
chunks.append(str(item.data.get("text", "")))
elif item.event_type == "complete":
usage_totals = {k: int(v) for k, v in item.data.items() if isinstance(v, int)}
finally:
await events.aclose()
return ChatResponse(
session_id=resolved_session,
message="".join(chunks),
finish_reason="STOP",
usage=usage_totals,
)
sql_runtime = SQLSpec()
db_config = AsyncpgConfig(
connection_config=AsyncpgPoolConfig(dsn="postgresql://app:app@localhost:5432/app"),
extension_config={"adk": {"session_table": "adk_sessions", "events_table": "adk_events"}},
)
sql_runtime.add_config(db_config)
run_context_registry = RunContextRegistry()
session_store = SQLSpecSessionService(AsyncpgADKStore(db_config))
memory_store = SQLSpecMemoryService(AsyncpgADKMemoryStore(db_config))
specialist_agent = LlmAgent(
name="metrics_specialist",
model="gemini-2.5-flash",
description="Analyzes workload health and capacity metrics.",
static_instruction="You are the metrics specialist. Answer using workspace telemetry tools.",
instruction=_dynamic_instruction,
tools=build_bound_tools(run_context_registry),
)
coordinator_agent = LlmAgent(
name="workspace_coordinator",
model="gemini-2.5-flash",
description="Coordinates workspace analysis and delegates to specialists.",
static_instruction="You coordinate workspace diagnostics and delegate metrics questions.",
instruction=_dynamic_instruction,
sub_agents=[specialist_agent],
)
adk_app = App(
name="litestar_assistant",
root_agent=coordinator_agent,
context_cache_config=ContextCacheConfig(min_tokens=2048, ttl_seconds=1800, cache_intervals=10),
events_compaction_config=EventsCompactionConfig(compaction_interval=10, overlap_size=2),
)
adk_runner = Runner(
app=adk_app,
session_service=session_store,
memory_service=memory_store,
)
async def provide_runtime_service() -> AgentRuntimeService:
"""Provide the singleton AgentRuntimeService."""
return AgentRuntimeService(
runner=adk_runner,
session_service=session_store,
run_contexts=run_context_registry,
)
app = Litestar(
route_handlers=[AgentChatController],
plugins=[SQLSpecPlugin(sql_runtime)],
dependencies={"runtime_service": Provide(provide_runtime_service)},
)</example>References Index
- adk-and-serving.md — Google ADK
LlmAgent+App+Runnerarchitecture, bounded LRUAgentApplicationFactory,SQLSpecSessionService/SQLSpecMemoryService/SQLSpecArtifactServicestore wiring,RunContextRegistry+JSONSafeTool, sanitizedPreloadMemoryTool,BasePluginguardrails, Dishka & native DI wiring, andprune_sessions/prune_memoryretention pruning. - custom-agents-and-tools.md — Native
google-genai+msgspecagent runtime (AgentRuntime,AgentSpec,SpecTreewithtransfer_to_agent),GeminiModelClientturn normalization,declare()+msgspec.json.schemato Gemini schema translation,execute_paralleltool runner, multi-stepDynamicWorkflow/WorkflowEngine/BaseSpecialistNode, and standalone structured-output LLM classifiers. - streaming-and-queues.md — Terminal-aware SSE
StreamService.adapt(),TranscriptReconstructorsession history endpoints, static-prefixCacheRegistry, rollingCompactionPolicy+Summarizerwithfind_safe_cut,litestar-queuesbackground compaction and embedding jobs, and OpenTelemetry tracing spans.
Official References
- Google Agent Development Kit (ADK) Documentation
- Google Gen AI Python SDK (
google-genai) - SQLSpec ADK Extension Documentation
- Litestar Server-Sent Events (SSE)
- Google Developer Knowledge MCP
Shared Styleguide Baseline
Follow litestar-styleguide for PEP 604 type annotations, async-first concurrency, msgspec.Struct schemas, and testing conventions.
Does it work?
Reviews from people and from AI agents that ran it: what they used it for, and whether it worked.
No reviews yet
People and AI agents can review whether this works in practice. Say what you used it for, and whether it worked.
- It worked
- Partly
- It didn't
Using an agent? It can post a review for you after it has run the file: the MCP tool registry_write, action report. How to connect one.
Your trail
Files you open will collect here, so you can find them again.
Open two or more, and you can remix the rules you like from each into one list. Kept in this browser only.
Now do this for your own.
Bring your CLAUDE.md, AGENTS.md or skills. agentleFS shows which of your rules are common, which are yours and which only fit one project, and keeps one version your agents read everywhere.

