View a markdown version of this page

Monitoring agent usage - Amazon WorkSpaces

Monitoring agent usage

Note

This feature is in public preview. Features and behavior are subject to change.

You can deliver agent usage logs from agent-ready WorkSpaces to Amazon CloudWatch Logs, Amazon S3, or Amazon Data Firehose in your AWS account. You set up delivery with the CloudWatch Logs delivery APIs. Each log record describes one model inference request that an agent made: which WorkSpace and user made the request, which model it used, how many tokens it consumed, and how the request ended. Records cover model requests only; connector calls aren't included.

Use these logs to attribute model usage to teams and users, build dashboards, and keep an audit trail of model requests. The logs cover model inference requests only. Calls that agents make to connectors aren't included. Administrative API calls are recorded in AWS CloudTrail. You can deliver the logs to CloudWatch Logs, Amazon S3, or Amazon Data Firehose. For charges, see Pricing.

Supported log destinations

Agent-ready WorkSpaces support the following log destinations:

  • Amazon CloudWatch Logs: for near-real-time queries with CloudWatch Logs Insights and dashboards.

  • Amazon S3: for long-term storage and querying with Amazon Athena.

  • Amazon Data Firehose: for streaming to other analytics tools.

Supported log types

Agent-ready WorkSpaces deliver the following log type:

  • AGENTIC_WS_USAGE_LOGS: one record per agent inference request, with token usage per WorkSpace and user.

Prerequisites

Agent-ready WorkSpaces are set up in your account in US East (Ohio) (us-east-2). Create the delivery source, destinations, and deliveries in us-east-2. A delivery in another Region is created without error but receives no records.

Set up IAM permissions

To set up delivery, the AWS Identity and Access Management (IAM) principal you use needs permission to let agent-ready WorkSpaces deliver logs for your account. It also needs the CloudWatch Logs permissions for your destination. The following IAM policy example grants the WorkSpaces permission.

{ "Version": "2012-10-17", "Statement": [{ "Sid": "AgentReadyWorkSpacesLogDeliveryPermissions", "Effect": "Allow", "Action": "workspaces:AllowVendedLogDeliveryForResource", "Resource": "arn:aws:workspaces:us-east-2:<account-id>:account/<account-id>" }] }

For example IAM policies with all the required permissions for your specific logging destination, see Enable logging from AWS services in the Amazon CloudWatch Logs User Guide.

Example: an IAM role for setting up log delivery

The following policy has every permission needed to set up delivery to all three destinations. It also includes the permissions to create the log group, transformer, and field index shown on this page. Attach it to the role or user that sets up delivery. Remove the statements for destinations you don't use, and replace the placeholders with your Region, account ID, bucket name, and stream name.

{ "Version": "2012-10-17", "Statement": [ { "Sid": "AllowWorkSpacesUsageLogDelivery", "Effect": "Allow", "Action": "workspaces:AllowVendedLogDeliveryForResource", "Resource": "arn:aws:workspaces:us-east-2:<account-id>:account/<account-id>" }, { "Sid": "ConfigureDelivery", "Effect": "Allow", "Action": [ "logs:PutDeliverySource", "logs:PutDeliveryDestination", "logs:CreateDelivery", "logs:GetDelivery*", "logs:DescribeDeliver*", "logs:DeleteDelivery*" ], "Resource": "*" }, { "Sid": "CloudWatchLogsDestination", "Effect": "Allow", "Action": ["logs:PutResourcePolicy", "logs:DescribeResourcePolicies", "logs:DescribeLogGroups"], "Resource": "*" }, { "Sid": "LogGroupSetup", "Effect": "Allow", "Action": [ "logs:CreateLogGroup", "logs:PutRetentionPolicy", "logs:PutTransformer", "logs:PutIndexPolicy" ], "Resource": "arn:aws:logs:us-east-2:<account-id>:log-group:/aws/vendedlogs/workspaces-usage:*" }, { "Sid": "S3Destination", "Effect": "Allow", "Action": ["s3:PutBucketPolicy", "s3:GetBucketPolicy"], "Resource": "arn:aws:s3:::<bucket-name>" }, { "Sid": "FirehoseDestination", "Effect": "Allow", "Action": "firehose:TagDeliveryStream", "Resource": "arn:aws:firehose:us-east-2:<account-id>:deliverystream/<delivery-stream-name>" }, { "Sid": "LogDeliveryServiceLinkedRole", "Effect": "Allow", "Action": "iam:CreateServiceLinkedRole", "Resource": "arn:aws:iam::<account-id>:role/aws-service-role/delivery.logs.amazonaws.com/AWSServiceRoleForLogDelivery", "Condition": { "StringEquals": { "iam:AWSServiceName": "delivery.logs.amazonaws.com" } } } ] }

Your Amazon S3 bucket might use server-side encryption with a customer managed AWS Key Management Service (AWS KMS) key (SSE-KMS). In that case, you must also allow the delivery.logs.amazonaws.com service principal in the key policy. This statement is for S3 buckets only. The following statement allows delivery.

{ "Effect": "Allow", "Principal": { "Service": "delivery.logs.amazonaws.com" }, "Action": ["kms:GenerateDataKey*", "kms:Decrypt"], "Resource": "*", "Condition": { "StringEquals": { "aws:SourceAccount": "<account-id>" }, "ArnLike": { "aws:SourceArn": "arn:aws:logs:us-east-2:<account-id>:*" } } }

CloudWatch Logs doesn't edit key policies. This is the one policy you add yourself. A log group encrypted with a KMS key is different. Its key policy allows the CloudWatch Logs Regional principal logs.us-east-2.amazonaws.com, as for any encrypted log group. A Firehose stream uses its own encryption settings. It needs no statement for delivery.logs.amazonaws.com.

Configure log subscription

You configure delivery with the CloudWatch Logs delivery APIs, not a WorkSpaces API.

  1. Create a delivery source with the PutDeliverySource CloudWatch Logs API operation. Give the delivery source a name and for resourceArn, specify the ARN of your account resource, arn:aws:workspaces:us-east-2:<account-id>:account/<account-id>. For logType, specify AGENTIC_WS_USAGE_LOGS.

    aws logs put-delivery-source --region us-east-2 \ --name workspaces-usage-source \ --resource-arn arn:aws:workspaces:us-east-2:<account-id>:account/<account-id> \ --log-type AGENTIC_WS_USAGE_LOGS

    Running PutDeliverySource again with the same name updates the source. Your account can have one delivery source for AGENTIC_WS_USAGE_LOGS, so a second source with a different name returns ConflictException. Find the existing source with aws logs describe-delivery-sources and use it in step 3.

  2. Create a delivery destination with the PutDeliveryDestination CloudWatch Logs API operation. Give the delivery destination a name and for destinationResourceArn, specify the ARN of your log group, S3 bucket or Firehose stream. Don't set outputFormat. This log type accepts only the default format, and CreateDelivery rejects any other value.

    For a CloudWatch Logs log group (Standard class):

    aws logs create-log-group --region us-east-2 --log-group-name /aws/vendedlogs/workspaces-usage --log-group-class STANDARD aws logs put-retention-policy --region us-east-2 --log-group-name /aws/vendedlogs/workspaces-usage --retention-in-days 180 aws logs put-delivery-destination --region us-east-2 \ --name workspaces-usage-cwl \ --delivery-destination-configuration 'destinationResourceArn=arn:aws:logs:us-east-2:<account-id>:log-group:/aws/vendedlogs/workspaces-usage:*'

    For an Amazon S3 bucket:

    aws logs put-delivery-destination --region us-east-2 \ --name workspaces-usage-s3 \ --delivery-destination-configuration 'destinationResourceArn=arn:aws:s3:::<bucket-name>'

    For an Amazon Data Firehose stream, use a stream with Direct PUT as its source and tag it LogDeliveryEnabled=true. CloudWatch Logs adds the tag when you create the delivery if your role is allowed to. Firehose then writes the records to the stream's own destination, such as Amazon S3, Amazon Redshift, Amazon OpenSearch Service, or an HTTP endpoint:

    aws firehose tag-delivery-stream --region us-east-2 --delivery-stream-name <delivery-stream-name> \ --tags Key=LogDeliveryEnabled,Value=true aws logs put-delivery-destination --region us-east-2 \ --name workspaces-usage-firehose \ --delivery-destination-configuration 'destinationResourceArn=arn:aws:firehose:us-east-2:<account-id>:deliverystream/<delivery-stream-name>'
  3. Create a delivery with the CreateDelivery CloudWatch Logs API operation, linking the delivery source to a delivery destination. Don't set recordFields. One delivery source can feed several deliveries. Create one delivery for each destination to send the same records to CloudWatch Logs, S3, and Firehose at the same time. When you create the delivery, CloudWatch Logs updates the destination's resource policy (log group or bucket) for you.

    # CloudWatch Logs aws logs create-delivery --region us-east-2 \ --delivery-source-name workspaces-usage-source \ --delivery-destination-arn arn:aws:logs:us-east-2:<account-id>:delivery-destination:workspaces-usage-cwl # Amazon S3, with Hive-compatible hourly partitions for Athena and AWS Glue aws logs create-delivery --region us-east-2 \ --delivery-source-name workspaces-usage-source \ --delivery-destination-arn arn:aws:logs:us-east-2:<account-id>:delivery-destination:workspaces-usage-s3 \ --s3-delivery-configuration '{"suffixPath":"{region}/{yyyy}/{MM}/{dd}/{HH}/","enableHiveCompatiblePath":true}' # Amazon Data Firehose aws logs create-delivery --region us-east-2 \ --delivery-source-name workspaces-usage-source \ --delivery-destination-arn arn:aws:logs:us-east-2:<account-id>:delivery-destination:workspaces-usage-firehose

Each destination receives the same OpenTelemetry Protocol (OTLP) record, one per line (see Log schema and format). The first delivery to Firehose in your account also creates the service-linked role AWSServiceRoleForLogDelivery. CloudWatch Logs uses this role to write to the stream.

Verify log delivery

Records arrive within about 5 minutes of agent activity. In CloudWatch Logs, look for the log stream agentic_ws_usage_logs/. In S3, objects arrive under AWSLogs/aws-account-id=<account-id>/workspaces/region=us-east-2/year=.../hour=.../, in files named <...>_agentic_ws_usage_logs_<...>.log.gz. For Firehose, check the stream's IncomingRecords metric, then its destination. No records appear until an agent in the account makes an inference request.

Log schema and format

Each record is an OTLP/JSON log record with the event name gen_ai.client.inference.usage, one per model inference request. The aws.workspaces.outcome field says how the request ended:

  • ok: the model's response was returned to the agent in full. Check http.response.status_code: the model's own errors, such as 429 (throttled) or 400, are also ok.

  • denied: the request was refused before it reached a model. aws.workspaces.error.type gives the reason, for example FeatureDisabled. Token counts are 0, and there is no model or HTTP status.

  • upstream_error: the request to the model failed (aws.workspaces.error.type is UpstreamError).

  • client_disconnected: the agent stopped waiting before the response finished, for example because the user interrupted it. Token counts cover what was used up to that point.

  • capped: the response was ended early. aws.workspaces.error.type gives the reason.

These logs cover model inference requests only. They don't include calls that agents make to connectors, or requests that were refused before the caller's account was identified.

Field Type Present Description
cloud.provider (resource) string All records Always aws
cloud.region (resource) string All records The Region that served the request
cloud.account.id (resource) string All records Your AWS account ID
timeUnixNano string All records When the request was made, in nanoseconds since the Unix epoch
observedTimeUnixNano string All records When the record was written
severityText / severityNumber string / int All records Always INFO / 9
traceId string All records Trace identifier of the request. It comes from the agent's W3C traceparent when the agent sends one. Otherwise it is derived from the request ID, or random
spanId string When the agent sends a W3C traceparent The span identifier from that traceparent, so records join your own traces
eventName string All records Always gen_ai.client.inference.usage
gen_ai.request.model string When a model was identified The model the request used
gen_ai.usage.input_tokens int All records Input tokens observed so far, not counting cached tokens. 0 on denied records
gen_ai.usage.output_tokens int All records Output tokens observed so far. 0 on denied records
gen_ai.usage.cache_read.input_tokens int All records Input tokens read from the prompt cache. 0 on denied records
gen_ai.usage.cache_creation.input_tokens int All records Input tokens written to the prompt cache. 0 on denied records
user.id string Usually The WorkSpace user's user name. It is unique within a WorkSpace, so read it together with aws.workspaces.workspace.id. Absent on requests refused before the user was identified, such as throttled requests
aws.workspaces.workspace.id string All records The WorkSpace that made the request
aws.workspaces.request.id string All records Request identifier. It can be - when no request ID was available
aws.workspaces.inference.requests int All records Inference requests this record covers (1)
aws.workspaces.outcome string All records ok, denied, upstream_error, client_disconnected or capped. How the request ended (see the preceding list)
aws.workspaces.error.type string Not on ok Why the request didn't complete, for example FeatureDisabled, ModelNotFound, CallerThrottled or UpstreamError
http.response.status_code int When the model returned a status The HTTP status that the model returned. It isn't the status returned to the agent
aws.workspaces.streamed boolean All records Whether the response was streamed
aws.workspaces.duration_ms int All records Request duration in milliseconds

Attributes are a list of {key, value} pairs. A field that doesn't apply to a record is left out rather than set to null, so attribute positions differ between records. Address attributes by name (see Query usage logs), not by position.

The following is an example of a log for a successful request:

{ "resourceLogs": [{ "resource": { "attributes": [ { "key": "cloud.provider", "value": { "stringValue": "aws" } }, { "key": "cloud.region", "value": { "stringValue": "us-east-2" } }, { "key": "cloud.account.id", "value": { "stringValue": "111122223333" } } ] }, "scopeLogs": [{ "scope": { "name": "aws.workspaces.tokenmetering", "version": "1.0.0" }, "logRecords": [{ "timeUnixNano": "1794347329985940701", "observedTimeUnixNano": "1794347332839725478", "severityNumber": 9, "severityText": "INFO", "traceId": "9805cf53bf5a435fb44ef8ef3d7e2aa7", "eventName": "gen_ai.client.inference.usage", "attributes": [ { "key": "gen_ai.request.model", "value": { "stringValue": "anthropic.claude-sonnet-5" } }, { "key": "gen_ai.usage.input_tokens", "value": { "intValue": "2" } }, { "key": "gen_ai.usage.output_tokens", "value": { "intValue": "247" } }, { "key": "gen_ai.usage.cache_read.input_tokens", "value": { "intValue": "0" } }, { "key": "gen_ai.usage.cache_creation.input_tokens", "value": { "intValue": "11019" } }, { "key": "user.id", "value": { "stringValue": "user-1" } }, { "key": "aws.workspaces.workspace.id", "value": { "stringValue": "ws-example01" } }, { "key": "aws.workspaces.request.id", "value": { "stringValue": "9805cf53-bf5a-435f-b44e-f8ef3d7e2aa7" } }, { "key": "aws.workspaces.inference.requests", "value": { "intValue": "1" } }, { "key": "aws.workspaces.outcome", "value": { "stringValue": "ok" } }, { "key": "http.response.status_code", "value": { "intValue": "200" } }, { "key": "aws.workspaces.streamed", "value": { "boolValue": true } }, { "key": "aws.workspaces.duration_ms", "value": { "intValue": "2853" } } ] }] }] }] }

The following is an example of a log for a denied request. The requested model isn't allowed for this WorkSpace, so the request never reached a model:

{ "resourceLogs": [{ "resource": { "attributes": [ { "key": "cloud.provider", "value": { "stringValue": "aws" } }, { "key": "cloud.region", "value": { "stringValue": "us-east-2" } }, { "key": "cloud.account.id", "value": { "stringValue": "111122223333" } } ] }, "scopeLogs": [{ "scope": { "name": "aws.workspaces.tokenmetering", "version": "1.0.0" }, "logRecords": [{ "timeUnixNano": "1794342385872010223", "observedTimeUnixNano": "1794342385875607070", "severityNumber": 9, "severityText": "INFO", "traceId": "5ec6ea8be1d64ec693dcfdd3a8f5cd2b", "eventName": "gen_ai.client.inference.usage", "attributes": [ { "key": "gen_ai.usage.input_tokens", "value": { "intValue": "0" } }, { "key": "gen_ai.usage.output_tokens", "value": { "intValue": "0" } }, { "key": "gen_ai.usage.cache_read.input_tokens", "value": { "intValue": "0" } }, { "key": "gen_ai.usage.cache_creation.input_tokens", "value": { "intValue": "0" } }, { "key": "user.id", "value": { "stringValue": "user-1" } }, { "key": "aws.workspaces.workspace.id", "value": { "stringValue": "ws-example02" } }, { "key": "aws.workspaces.request.id", "value": { "stringValue": "5ec6ea8b-e1d6-4ec6-93dc-fdd3a8f5cd2b" } }, { "key": "aws.workspaces.inference.requests", "value": { "intValue": "1" } }, { "key": "aws.workspaces.outcome", "value": { "stringValue": "denied" } }, { "key": "aws.workspaces.error.type", "value": { "stringValue": "FeatureDisabled" } }, { "key": "aws.workspaces.streamed", "value": { "boolValue": false } }, { "key": "aws.workspaces.duration_ms", "value": { "intValue": "3" } } ] }] }] }] }

Query usage logs

OTLP stores attributes as a list. Attributes are addressable only by position until you add a transformer that maps them by name. The following commands add a transformer and an index policy on the fields you filter on:

aws logs put-transformer --region us-east-2 --log-group-identifier /aws/vendedlogs/workspaces-usage --transformer-config '[ {"parseJSON":{}}, {"copyValue":{"entries":[ {"source":"resourceLogs.0.scopeLogs","target":"_s"}, {"source":"_s.0.logRecords","target":"_r"}, {"source":"_r.0.attributes","target":"_a"}]}}, {"listToMap":{"source":"_a","key":"key","valueKey":"value","target":"attrs","flatten":true,"flattenedElement":"first"}}, {"deleteKeys":{"withKeys":["_s","_r","_a"]}} ]' aws logs put-index-policy --region us-east-2 --log-group-identifier /aws/vendedlogs/workspaces-usage --policy-document \ '{"Fields":["attrs.aws.workspaces.workspace.id.stringValue","attrs.user.id.stringValue","attrs.gen_ai.request.model.stringValue"]}'

The transformer and the index policy apply to records ingested after you create them. The following CloudWatch Logs Insights query shows token usage by WorkSpace. It counts every outcome, because interrupted or capped responses still used tokens:

fields `attrs.aws.workspaces.workspace.id.stringValue` as workspace, `attrs.gen_ai.usage.input_tokens.intValue` as in_tokens, `attrs.gen_ai.usage.output_tokens.intValue` as out_tokens | stats sum(in_tokens) as input_tokens, sum(out_tokens) as output_tokens, count(*) as requests by workspace | sort output_tokens desc

The following query counts denied and capped requests by outcome, reason, and WorkSpace:

fields `attrs.aws.workspaces.outcome.stringValue` as outcome, `attrs.aws.workspaces.error.type.stringValue` as reason, `attrs.aws.workspaces.workspace.id.stringValue` as workspace | filter outcome in ["denied", "capped"] | stats count(*) as requests by outcome, reason, workspace | sort requests desc

Stop delivery

Deleting a delivery stops log delivery. Delete each delivery by running aws logs delete-delivery --id delivery-id. Then delete the delivery source and the delivery destinations.

Security considerations

Consider the following when you deliver agent usage logs:

  • Data: records contain WorkSpace and user identifiers and model names, not prompts or responses.

  • Encryption: encrypt the log group, bucket, or Firehose stream with a customer managed AWS KMS key if required.

  • Retention: set a retention policy on the log group, an S3 lifecycle rule, or retention on the Firehose stream's destination.