Microsoft's Copilot Cowork moves to usage-based billing and may tap DeepSeek
Back to Tutorials
aiTutorialbeginner

Microsoft's Copilot Cowork moves to usage-based billing and may tap DeepSeek

June 16, 20265 views4 min read

Learn how to create a basic AI assistant interface similar to Microsoft's Copilot Cowork, complete with usage tracking and API integration.

Introduction

In this tutorial, you'll learn how to set up and use a basic AI assistant interface that mimics the functionality of Microsoft's Copilot Cowork. We'll create a simple command-line interface that can interact with an AI model, similar to what Microsoft is doing with their Copilot service. This tutorial will cover setting up the environment, connecting to an AI API, and implementing usage tracking to simulate the move toward usage-based billing.

Prerequisites

  • A computer with internet access
  • Python 3.7 or higher installed
  • Basic understanding of command-line interfaces
  • An API key from an AI service (we'll use OpenAI's API as an example)

Step-by-step instructions

1. Setting up the Environment

1.1 Install Required Python Packages

First, we need to install the necessary Python packages. Open your terminal or command prompt and run:

pip install openai python-dotenv

Why? The openai package allows us to communicate with OpenAI's API, while python-dotenv helps us manage our API keys securely by storing them in a separate file.

1.2 Create a Project Directory

Create a new folder for your project:

mkdir copilot_assistant
 cd copilot_assistant

Why? Organizing your project files in a dedicated directory makes it easier to manage and prevents conflicts with other projects.

2. Setting Up Your API Key

2.1 Create a .env File

Create a new file named .env in your project directory:

touch .env

Add your OpenAI API key to this file:

OPENAI_API_KEY=your_actual_api_key_here

Why? Storing your API key in a separate file prevents accidentally sharing it in public repositories and makes it easier to manage multiple keys.

2.2 Create the Main Python Script

Create a file called copilot.py:

touch copilot.py

Open this file in your text editor and add the following code to load your environment variables:

import os
from dotenv import load_dotenv
from openai import OpenAI

# Load environment variables
load_dotenv()

# Initialize the OpenAI client
client = OpenAI(api_key=os.getenv('OPENAI_API_KEY'))

Why? This sets up the connection to the OpenAI API using your secure API key from the .env file.

3. Implementing Basic AI Interaction

3.1 Create a Simple Chat Function

Add the following function to your copilot.py file:

def chat_with_ai(prompt):
    try:
        response = client.chat.completions.create(
            model="gpt-3.5-turbo",
            messages=[
                {"role": "system", "content": "You are a helpful assistant."},
                {"role": "user", "content": prompt}
            ],
            max_tokens=150
        )
        return response.choices[0].message.content.strip()
    except Exception as e:
        return f"Error: {str(e)}"

Why? This function sends a user prompt to the AI model and returns the response. The max_tokens parameter limits the response length to prevent excessive usage.

3.2 Add Usage Tracking

Now, add a simple usage tracking mechanism to simulate the move to usage-based billing:

# Simple usage tracking
usage_count = 0


def track_usage(prompt):
    global usage_count
    usage_count += 1
    print(f"Usage count: {usage_count}")
    print(f"Prompt length: {len(prompt)} characters")

Why? This simulates how Microsoft might track usage to implement usage-based billing, where each interaction is counted and potentially charged.

4. Creating the Main Interface

4.1 Build the Interactive Loop

Add this main loop to your copilot.py:

def main():
    print("Welcome to Copilot Assistant!")
    print("Type 'quit' to exit.")
    
    while True:
        user_input = input("\nYou: ")
        
        if user_input.lower() in ['quit', 'exit']:
            print("Goodbye!")
            break
        
        # Track usage
        track_usage(user_input)
        
        # Get AI response
        response = chat_with_ai(user_input)
        print(f"Assistant: {response}")

if __name__ == "__main__":
    main()

Why? This creates an interactive chat loop where users can continuously ask questions, with each interaction being tracked for usage purposes.

5. Running Your AI Assistant

5.1 Execute the Script

Run your script from the command line:

python copilot.py

Why? This starts your AI assistant, allowing you to interact with it in real-time.

5.2 Test the Interface

Try asking simple questions like:

  • "What is artificial intelligence?"
  • "How do I learn Python?"
  • "Explain quantum computing"

Notice how the usage counter increments with each question you ask.

Summary

In this tutorial, you've created a basic AI assistant interface similar to Microsoft's Copilot Cowork. You've learned how to set up a Python environment, connect to an AI API, implement usage tracking, and create an interactive chat interface. While this is a simplified version, it demonstrates the core concepts behind how AI assistants like Copilot work, including the move toward usage-based billing models that Microsoft is implementing.

This project gives you a foundation for understanding how AI services operate and how companies like Microsoft are adapting their pricing models to be more sustainable in the long term.

Source: The Decoder

Related Articles