Short, practical recipes for using the GitHub Copilot SDK with .NET and C#. Each recipe is concise, copy-pasteable, and points to complete runnable examples.
await using var client = new CopilotClient();await client.StartAsync();var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" });// ... do work ...// client.StopAsync() is automatically called when exiting scope
Use the await using pattern for automatic cleanup. This ensures StopAsync() is called even if exceptions occur.
using GitHub.Copilot.SDK;var client = new CopilotClient();await client.StartAsync();// Create multiple independent sessionsvar session1 = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" });var session2 = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" });var session3 = await client.CreateSessionAsync(new SessionConfig { Model = "claude-sonnet-4.5" });// Each session maintains its own conversation historyawait session1.SendAsync(new MessageOptions { Prompt = "You are helping with a Python project" });await session2.SendAsync(new MessageOptions { Prompt = "You are helping with a TypeScript project" });await session3.SendAsync(new MessageOptions { Prompt = "You are helping with a Go project" });// Follow-up messages stay in their respective contextsawait session1.SendAsync(new MessageOptions { Prompt = "How do I create a virtual environment?" });await session2.SendAsync(new MessageOptions { Prompt = "How do I set up tsconfig?" });await session3.SendAsync(new MessageOptions { Prompt = "How do I initialize a module?" });// Clean upawait session1.DisposeAsync();await session2.DisposeAsync();await session3.DisposeAsync();await client.StopAsync();
Organize files by metadata using AI-powered grouping strategies.
using GitHub.Copilot.SDK;var client = new CopilotClient();await client.StartAsync();var session = await client.CreateSessionAsync(new SessionConfig { Model = "gpt-5" });// Ask AI to organize filesvar done = new TaskCompletionSource<string>();session.On(evt =>{ if (evt is AssistantMessageEvent msg) { done.SetResult(msg.Data.Content); }});await session.SendAsync(new MessageOptions{ Prompt = @" I have these files in a directory: - report.pdf - meeting-notes.txt - photo.jpg - invoice.xlsx - presentation.pptx Please organize them into folders by type. Return JSON with the structure. "});var organizationPlan = await done.Task;Console.WriteLine(organizationPlan);// Parse and execute the organization plan// ...