Three-Layer Architecture
CopilotKit is built on a clean, event-driven three-layer architecture:The three-layer separation allows you to:
- Use any frontend framework (React, Angular, or Vanilla JS)
- Deploy your runtime on any Node.js server (Express, Hono, etc.)
- Integrate any agent framework (LangGraph, CrewAI, or build your own)
Architecture Diagram
Layer Responsibilities
Frontend Layer
The frontend layer manages the user interface and local application state:- CopilotKitCore - Central orchestrator managing agent registry, tool registry, context store, and event subscriptions
- Framework wrappers - React hooks, Angular services, or Vanilla JS API
- ProxiedAgent - Frontend representation of remote agents that translates calls into HTTP requests
- Frontend tools - Tool handlers that execute locally in the browser
- Context providers - Supply application state to agents via
useAgentContext
packages/v2/core/src/core/core.ts:179
Runtime Layer
The runtime layer is a server-side component that orchestrates agent execution:- CopilotRuntime - Central runtime class that receives HTTP requests and delegates to agents
- AgentRunner - Abstraction for managing thread/conversation state (in-memory or persistent)
- Middleware - Before/after request hooks for authentication, logging, and transformations
- Adapters - Express and Hono server adapters for easy integration
packages/v2/runtime/src/runtime.ts:57
The runtime can be deployed separately from your frontend, enabling microservice architectures and better scalability.
Agent Layer
The agent layer contains the AI logic and can be implemented with any framework:- BuiltInAgent - Default agent powered by Vercel AI SDK
- LangGraph agents - Use LangGraph for complex multi-step workflows
- CrewAI agents - Integrate CrewAI for multi-agent orchestration
- Custom agents - Implement the
AbstractAgentinterface for full control
Request Lifecycle
Understanding the complete request lifecycle helps you debug and optimize your application:1. Initialization Phase
packages/v2/core/src/core/agent-registry.ts
2. User Sends Message
3. HTTP Request to Runtime
A POST request is sent to the runtime with aRunAgentInput payload:
packages/v2/core/src/agent.ts:148
4. Runtime Processing
The runtime performs these steps in order:- Middleware -
beforeRequestMiddlewareruns for auth/logging - Agent resolution - Runtime looks up the requested agent
- Agent cloning - Creates an isolated instance for this request
- AgentRunner execution - Runner manages thread state and executes the agent
packages/v2/runtime/src/runtime.ts:66
5. SSE Stream Back to Frontend
The agent emits AG-UI events streamed to the frontend:- Run lifecycle events:
RUN_STARTED,RUN_FINISHED,RUN_ERROR - Message events:
TEXT_MESSAGE_START,TEXT_MESSAGE_CONTENT,TEXT_MESSAGE_END(streaming) - Tool events:
TOOL_CALL_START,TOOL_CALL_ARGS,TOOL_CALL_END,TOOL_CALL_RESULT - Step events:
STEP_STARTED,STEP_FINISHED
6. Frontend Tool Execution
When the agent calls a frontend tool:- Agent emits
TOOL_CALL_STARTwith tool name and arguments - Core looks up the handler in its tool registry
- Handler executes locally in the browser
- Result is sent back to runtime via HTTP
- Agent continues processing with the tool result
packages/v2/core/src/core/run-handler.ts
7. UI Update
As events arrive:- Core updates its message store
- Subscribers are notified
- React/Angular re-renders UI components
V1 vs V2 Architecture
Understanding the relationship between V1 and V2 is important for maintaining compatibility and planning migrations.
V2 Packages (Real Implementation)
V2 (@copilotkitnext/*) is the actual implementation where new features are built:
@copilotkitnext/shared- Common utilities, types, and constants@copilotkitnext/core- CopilotKitCore orchestrator (framework-agnostic)@copilotkitnext/react- React hooks and provider@copilotkitnext/angular- Angular services and signals@copilotkitnext/runtime- Server-side CopilotRuntime and AgentRunner@copilotkitnext/agent- BuiltInAgent implementation@copilotkitnext/voice- Voice input and transcription@copilotkitnext/web-inspector- Debug console for development@copilotkitnext/sqlite-runner- Persistent AgentRunner with SQLite
V1 Packages (Public API)
V1 (@copilotkit/*) wraps V2 internally for backward compatibility:
@copilotkit/react-core- Public<CopilotKit>provider and hooks (wraps V2)@copilotkit/react-ui- Chat UI components@copilotkit/react-textarea- AI-assisted text editing component@copilotkit/runtime- Server-side runtime with GraphQL server@copilotkit/runtime-client-gql- GraphQL client for frontend@copilotkit/sdk-js- Helpers for LangGraph/LangChain integration
Development Strategy: Build new features in V2 first. Add V1 wrappers only if backward compatibility is needed.
Next Steps
AG-UI Protocol
Learn about the event-based communication protocol
Agents
Understand ProxiedAgent, AbstractAgent, and agent implementations
Runtime
Deep dive into CopilotRuntime and AgentRunner
Frontend Integration
Explore hooks and frontend tools
