DEV Community

KAILAS VS
KAILAS VS

Posted on

How to Connect CopilotKit to a Python Backend Using Direct-to-LLM (FastAPI Guide)

AI copilots are rapidly becoming the primary interface for modern applications. Frameworks like CopilotKit make it easier to build production-grade, AI-powered assistants without manually handling raw LLM interactions or complex prompt pipelines.

In this guide, you’ll learn how to connect CopilotKit to a remote Python backend using Direct-to-LLM with FastAPI, and why this approach is often better than heavy orchestration tools like LangGraph.

What is CopilotKit?

CopilotKit is the Agentic Application Platform — an open-source framework with cloud and self-hosted services for building AI-powered, user-facing agentic applications.

It connects your application’s logic, state, UI, and context to agentic backends, enabling interactive experiences across embedded UIs and headless interfaces. Teams use CopilotKit to build, deploy, and operate agentic features that feel deeply integrated into their products.

CopilotKit supports:

  • Direct integration with any agentic backend
  • Connectivity via AG-UI, MCP, and A2A protocols
  • Native integrations with popular agent frameworks through AG-UI

By decoupling your application from specific models, frameworks, or agent protocols, CopilotKit allows you to evolve your AI stack without redesigning your product’s UX.

Why Use CopilotKit with Direct-to-LLM + Remote Python Backend?

Lightweight architecture (no heavy orchestration)

Many AI systems rely on orchestration frameworks like LangGraph or middleware pipelines, which introduce:

  • More infrastructure
  • Higher latency
  • More maintenance complexity

With CopilotKit Direct-to-LLM, you keep things simple:

**CopilotKit → UI + LLM + intent handling

Python (FastAPI) → data + business logic + integrations**

Best for streaming AI responses

Direct-to-LLM is ideal when you need:

  • Real-time AI streaming responses
  • Low-latency conversational AI
  • Smooth user experience

This works especially well for:

  • Customer support copilots
  • Booking / planning assistants
  • SaaS dashboard copilots
  • Data analytics copilots

Reuse your existing Python backend

  • Most teams already use:
  • FastAPI / Django / Flask
  • PostgreSQL / MySQL / MongoDB
  • Python-based ML models

CopilotKit’s Remote Backend Endpoint lets you integrate all of this without rewriting your logic in Node.js.

*How CopilotKit’s Remote Backend Endpoint Works
*

Here’s the flow:

  1. User → CopilotKit
  2. CopilotKit → Python FastAPI backend
  3. Backend returns structured JSON
  4. CopilotKit → Direct-to-LLM
  5. LLM streams response back to user

Setting Up a FastAPI Remote Endpoint for CopilotKit

1️⃣ Install dependencies

poetry new My-CopilotKit-Remote-Endpoint
cd My-CopilotKit-Remote-Endpoint
poetry add copilotkit fastapi uvicorn
Enter fullscreen mode Exit fullscreen mode

2️⃣ Create FastAPI server

Create server.py:

from fastapi import FastAPI

app = FastAPI()
Enter fullscreen mode Exit fullscreen mode

3️⃣ Define a CopilotKit backend action

from fastapi import FastAPI
from copilotkit.integrations.fastapi import add_fastapi_endpoint
from copilotkit import CopilotKitRemoteEndpoint, Action as CopilotAction

app = FastAPI()

async def fetch_name_for_user_id(userId: str):
    return {"name": "User_" + userId}

action = CopilotAction(
    name="fetchNameForUserId",
    description="Fetches user name from the database for a given ID.",
    parameters=[
        {
            "name": "userId",
            "type": "string",
            "description": "The ID of the user to fetch data for.",
            "required": True,
        }
    ],
    handler=fetch_name_for_user_id
)

sdk = CopilotKitRemoteEndpoint(actions=[action])

add_fastapi_endpoint(app, sdk, "/copilotkit_remote")

def main():
    import uvicorn
    uvicorn.run("server:app", host="0.0.0.0", port=8000, reload=True)

if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode

Run the server:

poetry run python server.py

Your endpoint will be available at:

http://localhost:8000/copilotkit_remote

*Connecting to Copilot Cloud
*

  1. Go to Copilot Cloud dashboard
  2. Register your FastAPI endpoint as a Remote Endpoint
  3. Use either:
  4. Local tunnel, or
  5. Hosted backend URL

CopilotKit will now call your Python backend automatically.

Advanced: Thread Pool Configuration

add_fastapi_endpoint(app, sdk, "/copilotkit_remote", max_workers=10)
Enter fullscreen mode Exit fullscreen mode

Useful for high-traffic applications.

Dynamic Agents with CopilotKit

Frontend:

<CopilotKit properties={{ someProperty: "xyz" }}>
  <YourApp />
</CopilotKit>
Enter fullscreen mode Exit fullscreen mode

Backend:

def build_agents(context):
    return [
        LangGraphAgent(
            name="some_agent",
            description="This agent does something",
            graph=graph,
            langgraph_config={
                "some_property": context["properties"]["someProperty"]
            }
        )
    ]

app = FastAPI()
sdk = CopilotKitRemoteEndpoint(agents=build_agents)
Enter fullscreen mode Exit fullscreen mode

Real-World Use Case (In-Body Example)

In a recent booking-related AI copilot project, I used CopilotKit Direct-to-LLM with a FastAPI backend to deliver real-time, streaming AI responses without complex orchestration like LangGraph.

Flow:

  • User asks a question
  • CopilotKit calls FastAPI → fetches structured data
  • CopilotKit sends data directly to LLM
  • LLM streams response in real time

This kept the system simple, fast, and maintainable.

When Should You Use This Architecture?

Use this pattern when:

  • You already have a Python backend
  • You need real-time streaming responses
  • You want to avoid complex orchestration
  • You need production-ready scalability

Conclusion

Using CopilotKit Direct-to-LLM with a Remote Python Backend gives you:

✔ FastAPI integration
✔ Real-time streaming AI
✔ Minimal orchestration
✔ Clean system design
✔ Production-ready architecture

If you’re building AI copilots today, this pattern is worth adopting.

Top comments (1)

Collapse
 
sreekailas_vs_25539329436 profile image
Sreekailas VS

Great