Pular para o conteúdo

Logging

Atualizado em Ver como Markdown

Logging is a fundamental building block for application development. Logs provide insights during the early stages of development and are often critical to understanding issues occurring in production.

Your AI Gateway dashboard shows logs of individual requests, including the user prompt, model response, provider, timestamp, request status, token usage, cost, duration, and the user agent of the client that made the request. When DLP policies are configured, logs for requests that trigger a DLP match also include the DLP action taken (Flag or Block), matched policy IDs, matched profile IDs, and the specific detection entries that were triggered. These logs persist, giving you the flexibility to store them for your preferred duration and do more with valuable request data.

Each gateway has a storage limit based on your plan. You can customize this limit per gateway in your gateway settings. If your storage limit is reached, new logs will stop being saved. To continue saving logs, you must delete older logs to free up space for new logs. To learn more about your plan limits, refer to Limits.

We recommend using an authenticated gateway when storing logs to prevent unauthorized access and protects against invalid requests that can inflate log storage usage and make it harder to find the data you need. Learn more about setting up an authenticated gateway.

Default configuration

Logs, which include metrics as well as request and response data, are enabled by default for each gateway. This logging behavior will be uniformly applied to all requests in the gateway. If you are concerned about privacy or compliance and want to turn log collection off, you can go to settings and opt out of logs. If you need to modify the log settings for specific requests, you can override this setting on a per-request basis.

To change the default log configuration in the dashboard:

  1. In the Cloudflare dashboard, go to the AI Gateway page.

    Go to AI Gateway ↗
  2. Select Settings.

  3. Change the Logs setting to your preference.

Per-request logging

To override the default logging behavior set in the settings tab, you can define headers on a per-request basis.

Collect logs (cf-aig-collect-log)

The cf-aig-collect-log header allows you to bypass the default log setting for the gateway. If the gateway is configured to save logs, the header will exclude the log for that specific request. Conversely, if logging is disabled at the gateway level, this header will save the log for that request.

In the example below, we use cf-aig-collect-log to bypass the default setting to avoid saving the log.

# Run `wrangler whoami` to get your account ID to replace $CLOUDFLARE_ACCOUNT_ID,
# and `wrangler auth token` to get an auth token to replace $CLOUDFLARE_API_TOKEN.
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --header "cf-aig-collect-log: false" \
  --data '{
    "model": "openai/gpt-4.1-mini",
    "messages": [
      {
        "role": "user",
        "content": "What is the email address and phone number of user123?"
      }
    ]
  }'

Collect log payload (cf-aig-collect-log-payload)

The cf-aig-collect-log-payload header allows you to control whether the raw request and response bodies (payloads) are stored for a given request. Unlike cf-aig-collect-log, which controls the entire log entry, this header only affects payload storage — metadata such as token counts, model, provider, status code, cost, and duration will still be logged.

This is useful when you want to maintain visibility into usage metrics and request metadata without persisting sensitive prompt or completion data.

Header value Behavior
true Request and response payloads are stored.
false Payload storage is skipped. Metadata-only log entries are still saved.

In the example below, we use cf-aig-collect-log-payload to skip storing the request and response bodies while keeping the metadata log.

# Run `wrangler whoami` to get your account ID to replace $CLOUDFLARE_ACCOUNT_ID,
# and `wrangler auth token` to get an auth token to replace $CLOUDFLARE_API_TOKEN.
curl -X POST "https://api.cloudflare.com/client/v4/accounts/$CLOUDFLARE_ACCOUNT_ID/ai/v1/chat/completions" \
  --header "Authorization: Bearer $CLOUDFLARE_API_TOKEN" \
  --header "Content-Type: application/json" \
  --header "cf-aig-collect-log-payload: false" \
  --data '{
    "model": "openai/gpt-4.1-mini",
    "messages": [
      {
        "role": "user",
        "content": "What is the email address and phone number of user123?"
      }
    ]
  }'

DLP fields in logs

When Data Loss Prevention (DLP) policies are enabled on a gateway, log entries for requests that trigger a DLP policy match include additional fields:

Field Description
DLP Action The action taken by the DLP policy: FLAG or BLOCK
DLP Policies Matched The IDs of the DLP policies that matched
DLP Profiles Matched The IDs of the DLP profiles that triggered within each matched policy
DLP Entries Matched The specific detection entry IDs that matched within each profile
DLP Check Whether the match occurred in the REQUEST, RESPONSE, or both

These fields are available both in the dashboard log viewer and through the Logs API. You can filter logs by DLP Action in the dashboard to view only flagged or blocked requests. For more details on DLP monitoring, refer to Monitor DLP events.

Managing log storage

To manage your log storage effectively, you can:

  • Set Storage Limits: Configure a limit on the number of logs stored per gateway in your gateway settings to ensure you only pay for what you need.
  • Enable Automatic Log Deletion: Activate the Automatic Log Deletion feature in your gateway settings to automatically delete the oldest logs once the storage limit for your account is reached. This ensures new logs are always saved without manual intervention.

How to delete logs

To manage your log storage effectively and ensure continuous logging, you can delete logs using the following methods:

Automatic Log Deletion

​To maintain continuous logging within your gateway's storage constraints, enable Automatic Log Deletion in your Gateway settings. This feature automatically deletes the oldest logs once the storage limit for your account is reached, ensuring new logs are saved without manual intervention.

Manual deletion

To manually delete logs through the dashboard, navigate to the Logs tab in the dashboard. Use the available filters such as status, cache, provider, cost, or any other options in the dropdown to refine the logs you wish to delete. Once filtered, select Delete logs to complete the action.

See full list of available filters and their descriptions below:

Filter category Filter options Filter by description
Status error, status error type or status.
Cache cached, not cached based on whether they were cached or not.
Provider specific providers the selected AI provider.
AI Models specific models the selected AI model.
Cost less than, greater than cost, specifying a threshold.
Request type Workers AI Binding, WebSockets the type of request.
Tokens Total tokens, Tokens In, Tokens Out token count (less than or greater than).
Duration less than, greater than request duration.
Feedback equals, does not equal (thumbs up, thumbs down, no feedback) feedback type.
Metadata Key equals, does not equal specific metadata keys.
Metadata Value equals, does not equal specific metadata values.
Log ID equals, does not equal a specific Log ID.
Event ID equals, does not equal a specific Event ID.
DLP Action FLAG, BLOCK the DLP action taken on the request.
User Agent equals, does not equal, contains the user agent of the client that made the request.

API deletion

You can programmatically delete logs using the AI Gateway API. For more comprehensive information on the DELETE logs endpoint, check out the Cloudflare API documentation.