> ## Documentation Index
> Fetch the complete documentation index at: https://docs.nebuly.com/llms.txt
> Use this file to discover all available pages before exploring further.

# GitHub Copilot

> Sync GitHub Copilot IDE and agent turns into Nebuly via GitHub Copilot usage records

The GitHub Copilot integration is a self-hosted utility script that reads your enterprise's [Copilot usage records](https://docs.github.com/en/enterprise-cloud@latest/rest/copilot/copilot-usage-metrics?apiVersion=2026-03-10), rebuilds each conversation, and ingests every user/assistant turn into Nebuly as an Interaction.

The data flow is:

Usage records (REST API, or audit log stream in S3 / Azure Blob) → request/response pairing → interaction assembly → Nebuly ingestion

The script is incremental and resumable: it tracks a local SQLite cache of already-synced data, so you can re-run it safely at any time without creating duplicates.

If your organization signs in with Microsoft Entra, the same script can enrich each interaction with directory data. See [Enrich with Microsoft Entra](#enrich-with-microsoft-entra).

The source repository is available [here](https://github.com/nebuly-ai/nebuly-integration-examples/tree/main/github_copilot).

> [!NOTE]
>
> Copilot usage records are a GitHub **public preview**. GitHub can change the record format, and the script drops records it cannot parse. Check the [Limitations](#limitations) before rolling this out.

## Prerequisites

* A **GitHub Enterprise Cloud** enterprise that uses **Enterprise Managed Users (EMU)**. Usage records are not available to other enterprise types.
* An **enterprise owner** who can create a token and change Copilot policies in **AI controls**.
* The **Copilot Usage Records API** policy set to **Enabled everywhere**, and **Copilot Usage Records Streaming** too if you want older history (see [Get your credentials](#get-your-credentials)).
* **Python ≥ 3.12** and [Poetry](https://python-poetry.org/docs/#installing-with-the-official-installer) installed on the machine that runs the script.
* Optional: an audit log stream into **Amazon S3** or **Azure Blob Storage**, if you want history older than 48 hours. See [Backfill from an audit log stream](#backfill-from-an-audit-log-stream-optional).

Usage records cover Copilot in VS Code, Visual Studio, JetBrains IDEs, Eclipse, the Copilot CLI and the Copilot cloud agent. github.com chat, GitHub Mobile, Xcode, Slack and Teams do not appear in usage records, so they cannot be synced.

## Install & run

Clone the integration examples repository, install dependencies, configure your `.env` file, and run the sync:

```bash theme={null}
git clone https://github.com/nebuly-ai/nebuly-integration-examples.git
cd nebuly-integration-examples/github_copilot
poetry install
# Only if you enable Entra enrichment with ENTRA_FETCH=true:
# poetry install --with entra
cp .env.example .env
# Fill in NEBULY_API_KEY, GITHUB_TOKEN, and GITHUB_ENTERPRISE
poetry run python -m github_copilot_sync --sources rest
```

> [!WARNING]
>
> The REST API only returns the last 48 hours of usage records. The first run can backfill at most that window. To import older history, stream your audit log to S3 or Azure Blob and follow [Backfill from an audit log stream](#backfill-from-an-audit-log-stream-optional).

Three optional settings change what you see in Nebuly:

| Variable | Default | Effect |
| - | - | - |
| `RESOLVE_USERS` | `true` | Looks up each GitHub user's login and email so `end_user` is readable. Set to `false` to skip the lookups and send `github-user-<id>` instead. |
| `CONVERSATION_GAP_HOURS` | `8` | GitHub does not send a conversation id. When the same user, with the same opening message, is idle for longer than this, the script starts a new conversation. |
| `INTERACTION_IDLE_MINUTES` | `30` | How long a turn waits for a follow-up before it is sent to Nebuly. |

Follow the guide below to retrieve the necessary credentials.

For the complete environment variable reference, available CLI flags, payload customization and resumable-cache details, check the [project README](https://github.com/nebuly-ai/nebuly-integration-examples/tree/main/github_copilot).

## Get your credentials

The script requires three environment variables. Follow the steps below to enable usage records and obtain each value.

**1. Enable usage records in GitHub**

An **enterprise owner** turns the feature on for the whole enterprise.

1. In GitHub, open your enterprise and click **AI controls**, then **Copilot** in the sidebar.
2. Set the **Copilot Usage Records API** policy to **Enabled everywhere**. This allows the script to read records over REST.
3. If you also want to stream records to S3 or Azure Blob for older history, set the **Copilot Usage Records Streaming** policy to **Enabled everywhere**.

GitHub's REST reference also says the general **Copilot usage metrics** policy must be **Enabled everywhere** for its Copilot metrics endpoints. GitHub's usage records instructions list only the two policies above, but if the check below returns `403`, make sure that one is enabled too.

Policy labels can move while the feature is in preview. If you cannot find one of them, search the **Copilot** page in **AI controls** for "usage records" or check GitHub's [REST API endpoints for Copilot usage metrics](https://docs.github.com/en/enterprise-cloud@latest/rest/copilot/copilot-usage-metrics?apiVersion=2026-03-10) page.

**2. Nebuly secret key → NEBULY\_API\_KEY**

In Nebuly, go to **Settings → Projects → View Nebuly keys → Secret keys** and copy the secret key for the project you want to sync.

**3. GitHub token → GITHUB\_TOKEN**

The token must belong to an **enterprise owner**, because only enterprise owners can read usage records.

1. In GitHub, click your profile picture, then **Settings → Developer settings → Personal access tokens → Tokens (classic)**.
2. Click **Generate new token**, then **Generate new token (classic)**.
3. Give the token a name, for example `nebuly-copilot-sync`.
4. Choose an expiration that matches your rotation policy.
5. Select the **`read:enterprise`** scope. Do not grant other scopes: the script only reads.
6. Click **Generate token** and copy the value immediately; it is shown only once.

> [!WARNING]
>
> This token can read the prompts and responses of every Copilot user in the enterprise. Treat it like a root credential and store it in a secrets manager.

> [!NOTE]
>
> Resolving a user's email (`RESOLVE_USERS=true`) reads your enterprise's external identities, from SAML single sign-on or from OIDC (for example Microsoft Entra ID). An enterprise owner token with `read:enterprise` can see them. If the identities are not visible, the script logs one warning and uses the GitHub login as `end_user` instead.
>
> If your enterprise restricts personal access tokens (classic) under **Policies → Personal access tokens**, GitHub documents that policy as blocking access to the enterprise's organizations and does not say whether it affects enterprise endpoints. If you cannot create the token, or the check below returns `403` even with the right scope, ask your enterprise owner to review that policy.

**4. Enterprise slug → GITHUB\_ENTERPRISE**

Your enterprise slug is the last part of your enterprise URL: `https://github.com/enterprises/<slug>`. Copy `<slug>`. If your enterprise uses GHE.com data residency, the slug is the subdomain in `https://<slug>.ghe.com`, and you also set `GITHUB_API_URL=https://api.<slug>.ghe.com` in `.env`.

**5. Check access**

Request one usage record to confirm the token and the enterprise slug work together:

```bash theme={null}
curl -sS "https://api.github.com/enterprises/$GITHUB_ENTERPRISE/copilot/usage-records?per_page=1" \
  --header "Accept: application/vnd.github+json" \
  --header "Authorization: Bearer $GITHUB_TOKEN" \
  --header "X-GitHub-Api-Version: 2026-03-10"
```

A working setup returns a JSON array of usage records. The values below are placeholders, and the exact shape can change while the API is in preview:

```json theme={null}
[
  {
    "type": "request",
    "user_id": 123456,
    "enterprise_id": 7890,
    "endpoint": "<chat-completions-endpoint>",
    "body": "<JSON-encoded request body>",
    "@timestamp": 1790000000000,
    "event_id": "<event-id>",
    "github_request_id": "<request-id>"
  }
]
```

An empty array means no Copilot activity in the last 48 hours. A `401` means the token is wrong or expired. A `403` means the token is not an enterprise owner token, lacks the `read:enterprise` scope, or the policies from the first step are not enabled. A `404` usually means a wrong enterprise slug or an enterprise that is not eligible for usage records.

## Backfill from an audit log stream (optional)

GitHub keeps usage records on the REST API for 48 hours only. If you want to import older history, or you cannot run the script at least daily, stream your enterprise audit log into a bucket you own and let the script read from it. Streaming only captures activity from the moment you enable it, so set it up before you need the data.

Set up one of the two destinations below, then run the script with the matching source:

```bash theme={null}
poetry run python -m github_copilot_sync --sources rest,s3 --from-date 2026-11-01
```

Use `--sources rest,azure_blob` for Azure Blob Storage. Each source keeps its own progress, so adding a stream source does not resend what REST already read.

> [!TIP]
>
> The script lists every object under the prefix on every run. Point `S3_PREFIX` or `AZURE_BLOB_PREFIX` at the folder that holds the usage records, and archive or expire old objects, so the listing stays small.

### Amazon S3

**1. Create the bucket and let GitHub write to it**

Create an S3 bucket with public access blocked. To avoid storing long-lived AWS keys in GitHub, use **OpenID Connect**: add GitHub as an IAM OIDC provider and create a role that GitHub can assume, with `s3:PutObject` permission on the bucket's objects. GitHub's [Streaming the audit log for your enterprise](https://docs.github.com/en/enterprise-cloud@latest/admin/monitoring-activity-in-your-enterprise/reviewing-audit-logs-for-your-enterprise/streaming-the-audit-log-for-your-enterprise) guide lists the provider URL, audience and trust policy values.

**2. Configure the stream in GitHub**

1. In GitHub, open your enterprise and click **Settings → Audit log → Log streaming**.
2. Open the **Configure stream** dropdown and choose **Amazon S3**.
3. Under **Authentication**, choose **OpenID Connect** (recommended) or **Access keys**.
4. Choose the bucket region and enter the bucket name, plus the role ARN (OpenID Connect) or the access key ID and secret key.
5. Click **Check endpoint**, then **Save**.

Also make sure the **Copilot Usage Records Streaming** policy from [Get your credentials](#get-your-credentials) is turned on.

**3. Point the script at the bucket**

Set these in `.env`:

```bash theme={null}
S3_BUCKET=
S3_PREFIX=
```

The script reads AWS credentials from the standard AWS credential chain (`AWS_ACCESS_KEY_ID`, `AWS_SECRET_ACCESS_KEY`, `AWS_REGION`, or an instance role). Give that identity read-only access: `s3:ListBucket` on the bucket and `s3:GetObject` on its objects. Use a separate identity from the one GitHub writes with.

### Azure Blob Storage

**1. Create a write-only SAS URL for GitHub**

1. In the Azure portal, go to **Storage Accounts**, select your account, then **Data storage → Containers** and your container.
2. Under **Settings**, click **Shared access tokens**.
3. In **Permissions**, select only **Create** and **Write**.
4. Set an expiry date that matches your secret rotation policy.
5. Click **Generate SAS token and URL** and copy the **Blob SAS URL**.

**2. Configure the stream in GitHub**

1. In GitHub, open your enterprise and click **Settings → Audit log → Log streaming**.
2. Open the **Configure stream** dropdown and choose **Azure Blob Storage**.
3. Paste the **Blob SAS URL**. The container name fills in automatically.
4. Click **Check endpoint**, then **Save**.

Also make sure the **Copilot Usage Records Streaming** policy from [Get your credentials](#get-your-credentials) is turned on. Azure Government is not supported by GitHub audit log streaming.

**3. Point the script at the container**

Create a second, read-only credential for the script: either a SAS token with **Read** and **List** permissions, or a storage account connection string. Then set in `.env`:

```bash theme={null}
AZURE_BLOB_CONTAINER=
AZURE_BLOB_PREFIX=
# Either an account URL plus SAS token...
AZURE_BLOB_ACCOUNT_URL=
AZURE_BLOB_SAS_TOKEN=
# ...or a connection string
# AZURE_STORAGE_CONNECTION_STRING=
```

## What is synced

By default the script syncs one source, `rest`. Choose sources with `--sources`, a comma-separated list of `rest`, `s3` and `azure_blob`.

| Source | Where it reads from |
| - | - |
| `rest` | The enterprise usage records REST API, last 48 hours |
| `s3` | Usage records streamed into your S3 bucket |
| `azure_blob` | Usage records streamed into your Azure Blob container |

Only Copilot chat calls are synced. Completions, next edit suggestions and embeddings are skipped, and so are background helper calls such as chat title generation. When Copilot works as an agent and calls several tools in a row, those calls are grouped into one turn, so each interaction is one user request and the final answer.

`end_user` is the user's email when it can be resolved, otherwise their GitHub login, otherwise `github-user-<id>`.

Each interaction carries tags taken from the usage record. Empty values are omitted.

| Tag | Value |
| - | - |
| `endpoint` | The Copilot endpoint that served the request |
| `model`, `models` | The model that answered. `models` is used when a turn used more than one |
| `tools used` | Comma-separated tools the agent called in the turn |
| `truncated` | `true` when a usage record is flagged as truncated. GitHub's reference does not list this field yet, so it can be absent |

Tool calls made during the turn are attached to the interaction as retrieval traces. Input and output token counts are included when the record carries them. The script does not send a cost: Nebuly estimates it from the model and the token counts.

Message content longer than 8,000 characters is truncated.

## Export to CSV (optional)

To review the data before sending anything to Nebuly, write it to a local CSV file instead:

```bash theme={null}
CSV_EXPORT=true poetry run python -m github_copilot_sync --sources rest
```

`NEBULY_API_KEY` is not required in this mode and nothing is sent to Nebuly. Each interaction becomes one row, with the same fields, tags and traces that Nebuly would receive. The file defaults to `./interactions.csv`; set `CSV_EXPORT_PATH` to change it.

Rows are appended on every run, so use a new file to start fresh. Exporting to CSV keeps its own progress and does not mark data as sent: when you later switch to Nebuly, the data is sent again, as long as it is still within the 48-hour REST window or present in your audit log stream.

## Limitations

* **Public preview.** The usage records API and its record format can change. Records the script cannot parse are skipped.
* **48-hour REST window.** The REST API returns only the last 48 hours. Run the script at least daily, ideally hourly, or stream to S3 or Azure Blob for longer retention.
* **Conversations are rebuilt.** GitHub does not send a conversation id. The script derives one from the user and the opening message, split by `CONVERSATION_GAP_HOURS`. Two parallel conversations from the same user that start with the same text can share an id.
* **Not every surface.** github.com chat, GitHub Mobile, Xcode, Slack and Teams do not appear in usage records.
* **Cost is an estimate.** GitHub usage records have no spend field. Any cost in Nebuly is estimated from the model and tokens, and does not reflect GitHub's bill. Tokens appear only when the record includes usage.
* **Large records.** When a usage record is flagged as truncated, the interaction gets the `truncated` tag. GitHub's reference does not document that flag yet. Input and output sent to Nebuly are capped at 8,000 characters.
* **Helper calls.** A background call the script does not recognize is ingested as a normal turn. You can extend the filter, see the project README.
* **Email needs external identities.** Mapping a GitHub login to an email needs an enterprise owner token and an enterprise that signs in with SAML or OIDC. When the identities are not visible, `end_user` is the login.
* **Azure Event Hubs** and other stream destinations are not supported. Use S3 or Azure Blob Storage.
* **Team, market and tenure** are only available through the [Entra enrichment](#enrich-with-microsoft-entra).

## Enrich with Microsoft Entra

Usage records do not include team, market or tenure. If your GitHub users sign in with Microsoft, the sync can add them from your Entra directory. Register the Entra app and pick how the directory is loaded on the [Microsoft Entra](https://docs.nebuly.com/connectors/microsoft-entra/microsoft-entra.md) page, then set one of the two options in `.env`, not both:

```bash theme={null}
# Option 1: fetch at startup (run `poetry install --with entra` first)
ENTRA_FETCH=true
AZURE_TENANT_ID=
AZURE_CLIENT_ID=
AZURE_CLIENT_SECRET=

# Option 2: a pre-exported directory file
# ENTRA_EXTRACT_PATH=../microsoft_entra/entra.json
```

Turn this on before the first sync. Interactions that were already sent are not updated.

The join uses the email resolved from your enterprise's SAML or OIDC identities, so it needs `RESOLVE_USERS=true` (see [Limitations](#limitations)). When the email is resolved, `end_user` is already that email. See [What gets added](https://docs.nebuly.com/connectors/microsoft-entra/microsoft-entra.md#what-gets-added) for the field mapping.

## FAQ

**Where does the synced data appear?**\
Conversations imported by the script show up on the **Interactions** page in your Nebuly project.

**When will my data appear?**\
GitHub publishes usage records with a short delay, and there can be a further delay of up to 30 minutes between the moment the script sends data and when it becomes visible in Nebuly. A turn is also held until it is complete, which takes up to `INTERACTION_IDLE_MINUTES` (default 30). The script only syncs when you run it, so for ongoing sync schedule it (for example with cron). Because the REST API keeps only 48 hours, run it at least daily, ideally hourly.

**Is it safe to re-run the script?**\
Yes. The script keeps a local cache of which records and interactions have already been processed. The cache is specific to your enterprise, sources and Nebuly project, so pointing the script at a new Nebuly project sends the data again (within what GitHub still retains). Re-running it against the same project will pick up only new data and will not create duplicates. Delete the cache directory to start over.

**How can I see what would be sent without triggering ingestion?**\
Pass the `--dry-run` flag. The script will fetch and convert data but will skip the POST to Nebuly's ingestion endpoint, printing a summary instead.

**What happens if the script stops running for a while?**\
On the next run it continues from where it stopped. Data older than 48 hours is no longer on the REST API, so for gaps longer than that you need an audit log stream into S3 or Azure Blob. Streaming only captures activity from the day you turn it on.

**Are token and cost included?**\
Tokens are included when GitHub's record has them. Cost is not sent: Nebuly estimates it from the model and tokens, and it is not GitHub's billed amount. See [Limitations](#limitations).

**Why do some interactions have no team, market, or tenure?**\
Entra did not contain that user's email, or the email could not be resolved from GitHub. Enable enrichment before the first sync. A later run does not update an interaction that was already sent.

## Resources

> [card] [GitHub Copilot Nebuly integration](https://github.com/nebuly-ai/nebuly-integration-examples/tree/main/github_copilot)
>
> Full install guide, env-var table, CLI flags, and customization docs

> [card] [Copilot usage records API](https://docs.github.com/en/enterprise-cloud@latest/rest/copilot/copilot-usage-metrics?apiVersion=2026-03-10)
>
> GitHub's REST reference for the enterprise usage records endpoint

> [card] [Streaming the audit log](https://docs.github.com/en/enterprise-cloud@latest/admin/monitoring-activity-in-your-enterprise/reviewing-audit-logs-for-your-enterprise/streaming-the-audit-log-for-your-enterprise)
>
> How to stream your enterprise audit log to Amazon S3 or Azure Blob Storage

> [card] [Nebuly Interaction API](https://docs.nebuly.com/tracking/api-reference/events/post-events-interaction-with-trace-v2.md)
>
> API reference for the Interaction ingestion endpoint


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.