Historically, building an AI agent meant manually writing custom orchestration loops and stitching tools together from scratch. However, with the rise of sophisticated coding assistants, we can now leverage their production-ready SDKs to build our own custom agents. This allows us to tap into the exact same underlying infrastructure as your favorite AI tools—complete with native support for agent skills, memory, and robust tool execution.
The GitHub Copilot SDK is a powerful framework designed for this exact purpose. This step-by-step tutorial series will guide you from building a basic, foundational agent all the way to deploying an enterprise-class AI assistant.
Understanding the Architecture: Chatbots vs. Agents
When building a traditional chatbot, you typically make direct, stateless API calls to a LLM. This leaves you responsible for manually managing conversation history and writing custom loops to sustain a continuous dialogue.
An agent framework like the GitHub Copilot SDK abstracts this complexity away by structuring interactions into two core components:
- The Client (CopilotClient): Act as the bridge between your local environment and the AI infrastructure.
- The Session (via client.create_session): Represents a continuous, stateful interaction. Unlike a one-off API call, a session automatically retains context, manages conversation history, and tracks ongoing sub-tasks.
Step 1: Configure Your LLM Provider
Before spinning up our agent loop, we need to define our model and provider configuration. A major benefit of this setup is flexibility: you do not even need an active GitHub Copilot subscription to get started.
For this demonstration, we will connect to Gemini’s OpenAI-compatible endpoint using the lightweight and fast Gemini 3.1 Flash Lite model.
provider = {
"type": "openai",
"base_url": "https://generativelanguage.googleapis.com/v1beta/openai/",
"wire_api": "completions",
"api_key": os.getenv("GEMINI_API_KEY")
}
model="gemini-3.1-flash-lite"
Step 2: Initialize the Agent Loop
With our provider configured, we can now initialize the CopilotClient, establish a stateful session, and send our first asynchronous query.
question = "What is 2 + 2?"
async with CopilotClient() as client:
async with await client.create_session(
on_permission_request=PermissionHandler.approve_all,
model=model,
provider=provider
) as session:
response = await session.send_and_wait(question)
print(response.data.content)
Note: By setting PermissionHandler.approve_all, we are giving the agent permission to execute tools autonomously within this session context—a fundamental trait of true AI agency.
We have successfully configured and executed the very first stateful AI agent using the GitHub Copilot SDK in less than 20 lines of code. While a basic query is a great starting point, the real magic happens when we begin giving our agent actual autonomy.
If you want to see this code run live without setting up a local environment, click the badge below to jump straight into the interactive notebook and test it out.