CopilotKit integration
Turn your Blaxel agent (built with LangGraph) into an agent-native application in 10 minutes.
This tutorial will walk you through how to use CopilotKit to create complete copilots that leverage Blaxel agents in your frontend. Turn your MCP servers, models and agents hosted on Blaxel into CopilotKit CoAgents that provide full user interaction.
Requirements
- One or several MCP servers, hosted on Blaxel
- A model API, connected on Blaxel
Step 1: Create a LangGraph agent
You can quickly initialize a new project from scratch by using CLI command bl create-agent-app
. This will create a pre-scaffolded local repo where your entire code can be added. You can choose the base agentic framework for the template.
Develop your LangGraph agent, using Blaxel SDK to connect to Blaxel AI gateway for model APIs and MCP servers, and making sure to add a memory checkpointer. You can copy-paste the following code snippet into agent.py
to get started:
Step 2: Use CopilotKit integration to serve your agent
Next, use CopilotKit’s FastAPI integration to serve your LangGraph agent. You can directly modify main.py
from the scaffolded directory by copy-pasting the following code.
If you haven’t installed CopilotKit in your Python environment, do it now using uv:
Step 3: Setup CopilotKit
CopilotKit maintains a documentation on deploying CoAgents: we are now at Step 4 of this tutorial. The rest of this page will highlight the differences required to integrate with a Blaxel agent, using options “Self-Hosted Copilot Runtime” and “Self hosted (FastAPI)” for the code snippets when available.
Install CopilotKit
Make sure to have the latest packages for CopilotKit installed into your frontend.
Install Copilot Runtime
Copilot Runtime is a production-ready proxy for your LangGraph agents. In your frontend, go ahead and install it.
Step 4: Plug your agent to CopilotKit in your front-end
You have two options regarding hosting of the agent:
- local hosting
- hosting on Blaxel
Local hosting
Run the following command at the root of your agent folder to serve the agent locally:
The agent will be available on: http://localhost:1338/copilotkit
by default.
Now let’s setup a Copilot Runtime endpoint in your application and point your frontend to it. The following tutorial will demonstrate integration with a NextJS application. Check out CopilotKit’s documentation (step 6) for other frameworks.
Create the following route file:
You can now follow the rest of CopilotKit’s documentation on how to setup a Copilot in your frontend application (step 8). Make sure to adapt the name of the agent if you changed it.
Hosting on Blaxel
Deploy the agent
Run the following command at the root of your agent folder to deploy the agent to Blaxel:
Retrieve the base invocation URL for the agent. It should look like this, on top of which you will add the /copilotkit
endpoint.
Create API key
Then, create an API key either for your profile or for a service account in your workspace. Store that API key for the next step.
Integrate with CopilotKit
Now let’s setup a Copilot Runtime endpoint in your application and point your frontend to it. The following tutorial will demonstrate integration with a NextJS application. Check out CopilotKit’s documentation (step 6) for other frameworks.
Create the following route file. Make sure to replace https://run.blaxel.ai/{YOUR-WORKSPACE}/agents/{YOUR-AGENT}/copilotkit
and <API_KEY / TOKEN>
.
/copilotkit
endpoint on your agent (if left as default), or the actual endpoint name that you have used.You can now follow the rest of CopilotKit’s documentation on how to setup a Copilot in your frontend application (step 8). Make sure to adapt the name of the agent if you changed it.
Was this page helpful?