A developer cranks a cable reel feeding a COPILOT RUNTIME module on a gantry, wired to TOOLS, EVENTS, and PERMISSIONS consoles and a Python logo control unit.

Copilot SDK: Put a Coding Agent in Your Python Code

The Microsoft Copilot SDK, called the GitHub Copilot SDK before version 2.0, is a software development kit that lets you drive the agent runtime behind Copilot CLI from your own Python code. You install it with pip, open a session, and send a prompt. The agent then plans the work, calls tools, and edits files on its own. You can hand it custom Python functions, watch its progress live, and run it unattended as a CI check.

By the end of this tutorial, you’ll understand that:

  • The SDK exposes a programmable agent runtime that handles the tool loop, file edits, permissions, and session state for you.
  • The @define_tool decorator and a Pydantic model turn a regular Python function into a tool that the agent can call.
  • Enabling streaming and registering a handler with .on() give you live events for message deltas and tool calls.
  • A permission handler limits which reads, writes, and shell commands the agent may perform, so an unattended run in CI can review code without changing it.
  • Each prompt runs a full agent and is billed like a Copilot CLI prompt. For a single structured completion, a raw model call skips the agent’s overhead and costs less.

If you’ve used GitHub Copilot CLI, then you’ve already met this agent in your terminal. The SDK puts the same agent inside your Python programs. If you want a broader view of the tools in this space, then check out the AI coding agents guide.

In this tutorial, you’ll write a minimal agent and then extend it in three small scripts. Along the way, you’ll find out where the SDK fits well and where another tool would serve you better.

Take the Quiz: Test your knowledge with our interactive “Copilot SDK: Put a Coding Agent in Your Python Code” quiz. You’ll receive a score upon completion to help you track your learning progress:


Interactive Quiz

Copilot SDK: Put a Coding Agent in Your Python Code

Test your understanding of the Microsoft Copilot SDK, from running a Python agent to custom tools, live events, and read-only CI checks.

Get Started With the Microsoft Copilot SDK

The SDK’s README describes it as a library that:

[…] exposes the same engine behind Copilot CLI: a production-tested agent runtime you can invoke programmatically. (Source)

By default, the SDK loads that runtime into your Python process as a native library, so you never install or manage a separate server yourself.

Why not call a model API directly instead? You could, but then you’d have to build the agent around it yourself. Here’s what the SDK gives you out of the box:

  • The tool loop: The agent picks a tool, runs it, reads the result, and repeats until the task is done.
  • File edits and shell commands: Built-in tools view, search, and patch files and run shell commands in a working directory.
  • Permissions: A handler that you control approves or denies every read, write, or shell command that the agent requests.
  • Session state: Each session keeps the conversation, so follow-up prompts build on earlier turns.

To follow along, you need an active GitHub Copilot plan. The SDK also supports bring-your-own-key (BYOK) with your own model provider, which this tutorial doesn’t cover. Any Python version from 3.11 on works, but this tutorial uses Python 3.14. The SDK’s API is asynchronous, so some familiarity with async I/O in Python will help you read the examples. You should also understand virtual environments and type hints.

Install the SDK and Its Runtime

The demos let the agent write files and run shell commands, so you’ll run them inside a Docker container for security reasons.

Create a project folder and start a Python container that mounts it:

Language: Shell
$ mkdir copilot-sdk/
$ cd copilot-sdk/
$ docker run -it --rm -v "$PWD":/app -w /app python:3.14 bash

The -v option shares your project folder with the container as /app. You don’t need an editor inside the container. Write each script with your usual editor in copilot-sdk/ on your computer, and it shows up in /app right away. Then run it from the container’s shell, which you keep open for the rest of the tutorial. The --rm option deletes the container when you exit that shell.

Inside the container, create and activate a virtual environment, then install the SDK together with Pydantic, which you’ll use for tool parameters later:

Language: Shell
$ python -m venv .venv/
$ source .venv/bin/activate
(.venv) $ python -m pip install microsoft-copilot-sdk==2.0.6 \
    pydantic==2.14.4
(.venv) $ python -m copilot download-runtime --in-process

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

Locked learning resources

The full article is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Article

Already a member? Sign-In

About Leodanis Pozo Ramos

Leodanis is a self-taught Python developer, educator, and technical writer with over 10 years of experience.

» More about Leodanis

Each tutorial at Real Python is created by a team of developers so that it meets our high quality standards. The team members who worked on this tutorial are:

What Do You Think?

What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment below and let us know.

Commenting Tips: The most useful comments are those written with the goal of learning from or helping out other students. Get tips for asking good questions and get answers to common questions in our support portal.


Looking for a real-time conversation? Visit the Real Python Community Chat or join the next “Office Hours” Live Q&A Session. Happy Pythoning!

Become a Member to join the conversation.

Keep Learning

Related Topics: intermediate ai tools