We currently have a prototype that uses pydantic-ai and copilot-kit to validate and drive conversational actions. The prototype lacks robust session handling (conversation state, user context, ephemeral vs persistent sessions), persistence, concurrency control, rate limiting, and observability. We need to productionize it so sessions are correct, recoverable, secure, and scalable.
#Productionize Pydantic-AI + Copilot-Kit — add robust session handling, persistence, scaling, etc
1 messages · Page 1 of 1 (latest)
This solution has been AI generated
Here's a concrete path to productionize your Pydantic-AI + CopilotKit stack. It covers session correctness, persistence, concurrency/rate limits, observability, and user context using CopilotKit runtime + shared state, with custom adapters where needed.
Architecture overview
- CopilotRuntime for request routing and session isolation.
- Shared State for synchronized conversational state between UI and agent.
- External store (e.g., Redis/Postgres/Mongo) for durable persistence.
- Middleware/services for concurrency control and rate limiting.
- Observability hooks (CopilotKit premium) for logs/metrics/traces.
Session handling and state
- Use
agentSessioninCopilotRuntime.processAgentRequestto isolate sessions:agentName,threadId(back-compat),nodeNamefor routing.
import { CopilotRuntime } from "@copilotkit/runtime";
const runtime = new CopilotRuntime({ publicApiKey: process.env.COPILOTKIT_PUBLIC_KEY });
await runtime.processAgentRequest({
agentSession: { agentName: "assistant", threadId: req.body.threadId, nodeName: "node-a" },
input: req.body.message,
forwardedParameters: { userId: req.user.id }
});
- Use Pydantic-AI Shared State for session continuity and two-way UI↔agent sync.
- Docs: Shared State Overview/Read/Write/Predictive Updates.
Persistence (ephemeral vs. persistent)
- Ephemeral: rely on shared state during active sessions.
- Persistent: implement a database adapter to store thread/state snapshots. Examples show MongoDB; adapt similarly for Redis (cache) or Postgres (relational persistence).
- Example: copilot-openai-mongodb-atlas-vector-search.
Concurrency control and rate limiting
- Not built-in. Add middleware/service-level controls (e.g., queueing, per-user quotas). Provider rate-limit errors are surfaced by runtime; handle and backoff in your middleware.
Observability
- Use premium observability hooks for request/response/error events and progress indicators. Requires
publicApiKeyconfiguration.- Docs: Premium Overview, Observability.
User context
- Pass identity/tenant via
forwardedParametersor GraphQL context for downstream tools/LLM options.
References
- Shared State: https://docs.copilotkit.ai/docs/pydantic-ai/shared-state/index.mdx
- Observability: https://docs.copilotkit.ai/docs/pydantic-ai/premium/observability.mdx
- Example (DB integration): https://github.com/CopilotKit/CopilotKit/tree/main/examples/copilot-openaI-mongodb-atlas-vector-search
- CopilotRuntime source: packages/runtime/src/lib/runtime/copilot-runtime.ts
Was this helpful?
If this solution worked for you, please click on the appropriate option below to help us improve:
Hi @trim ferry, the latest version of CopilotKit comes with persistence built in. Could you give it a try and let us know what you think?
Hey @faint raven ,
just updated it to latest.
Will let you know.