docs/content/docs/integrations/langgraph/auth.mdx
CopilotKit supports user authentication for LangChain agents in two deployment modes:
@auth.authenticate decoratorBoth approaches enable your agents to access authenticated user context and implement proper authorization.
sequenceDiagram
participant Frontend
participant CopilotKit
participant LangGraphAgent
participant Agent
Frontend->>CopilotKit: authorization: "user-token"
CopilotKit->>LangGraphAgent: Forward auth token
LangGraphAgent->>Agent: user info via config
Agent->>Agent: Access authenticated user context
Pass your authentication token via the properties prop:
<CopilotKit
runtimeUrl="/api/copilotkit"
properties={{
authorization: userToken, // Forwarded as Bearer token
}}
>
<YourApp />
</CopilotKit>
Note: For LangGraph Platform, the authorization property is forwarded as a Bearer token.
For agents deployed to LangGraph Platform, authentication works out of the box with the @auth.authenticate decorator.
# auth.py in your LangGraph Platform deployment
from langgraph_sdk import Auth
auth = Auth()
@auth.authenticate
async def authenticate(authorization: str | None):
if not authorization or not authorization.startswith("Bearer "):
raise Auth.exceptions.HTTPException(status_code=401, detail="Unauthorized")
token = authorization.replace("Bearer ", "")
user_info = validate_your_token(token) # Your validation logic
return {
"identity": user_info["user_id"],
"role": user_info.get("role"),
"permissions": user_info.get("permissions", [])
}
async def my_agent_node(state: AgentState, config: RunnableConfig):
# Access user from LangGraph Platform authentication
user_info = config["configuration"]["langgraph_auth_user"]
user_id = user_info["identity"]
user_role = user_info.get("role")
# Your agent logic with user context
return state
For complete implementation details, see the LangGraph Platform Authentication documentation.
For self-hosted agents, you need to manually configure authentication context through dynamic agent creation.
# demo.py - Configure agent with authentication context
from copilotkit import CopilotKitRemoteEndpoint, LangGraphAgent
sdk = CopilotKitRemoteEndpoint(
agents=lambda context: [
LangGraphAgent(
name="sample_agent",
description="Agent with authentication support",
graph=graph,
langgraph_config={
"configurable": {
"copilotkit_auth": context["properties"].get("authorization")
}
}
)
],
)
async def my_agent_node(state: AgentState, config: RunnableConfig):
# Handle authentication for self-hosted mode
auth_token = config["configurable"].get("copilotkit_auth")
if auth_token:
user_info = validate_your_token(auth_token)
user_id = user_info["user_id"]
user_role = user_info.get("role")
else:
user_id = "anonymous"
user_role = None
# Your agent logic with user context
return state
For agents that work in both environments, use this pattern:
async def my_agent_node(state: AgentState, config: RunnableConfig):
user_id = "anonymous"
user_role = None
# LangGraph Platform mode
if "configuration" in config and "langgraph_auth_user" in config["configuration"]:
user_info = config["configuration"]["langgraph_auth_user"]
user_id = user_info["identity"]
user_role = user_info.get("role")
# Self-hosted mode
elif "configurable" in config and "copilotkit_auth" in config["configurable"]:
auth_token = config["configurable"]["copilotkit_auth"]
if auth_token:
user_info = validate_your_token(auth_token)
user_id = user_info["user_id"]
user_role = user_info.get("role")
# Your agent logic with user context
return state
@auth.authenticate handlerFor comprehensive authentication patterns, authorization handlers, and security best practices, refer to the LangGraph Platform Authentication documentation.
Token not reaching agent:
authorization in the properties propInvalid token format:
Bearer prefix for LangGraph PlatformUser info not available:
@auth.authenticate handler is properly configuredcopilotkit_auth is properly passed in langgraph_configAuthentication works locally but not in production: