Skip to main content

API Overview

The Log Query API returns a detailed record of every API call made under your account, including the model used, the actual amount charged, latency, whether the call was streamed, and the error code when a call fails. It complements the Balance Query API: balance query tells you how much credit is left, log query tells you where it went. Three typical use cases:

Automated reconciliation

Aggregate actual spend by time range or by model and reconcile it against your own billing

Self-service troubleshooting

Inspect error codes on failed requests to tell parameter problems from upstream problems

Support tickets

Provide the request_id to support so they can pinpoint the exact call
Logs are also viewable in the console under the Logs page. This API is the programmatic entry point to the same data, intended for automated reconciliation, scheduled exports, or feeding your own monitoring. For manual inspection, use the console — see How to view my call records.

How to Get Your System Token

The Log Query API authenticates with a System Token, which is not the same thing as an API key (see Important Notes at the end of this page).
1

Access Console

Visit api.apiyi.com/account/profile to access your profile page
2

Find System Token

Locate the “Account Options - System Token” section at the bottom of the page
3

Generate AccessToken

Enter your account password to receive an AccessToken that can be used for subsequent API queries
Get System Token

API Information

Request Details

Request Headers

Query Parameters

The effective cap on page_size is 10. Passing page_size=100 still returns only 10 records — this is not an error, and it is easy to misread as “I only made 10 calls in this period.” Any meaningful time range requires pagination, looping until the response returns an empty array. The Python and Node.js examples below already handle this.

Log Types

Always pass type=2 when calculating spend. Without it, top-up and system-grant records are returned as well. Their quota is 0, but model_name and token_name are also empty, so naively summing or grouping by model will produce wrong results.

Response Details

Success Response Example

Key Response Fields

The other field holds a JSON string, not a nested object, so it needs a second parse (json.loads() in Python, JSON.parse() in JavaScript). It contains billing_type, request_path (the endpoint actually called), group_ratio, model_ratio, and usage.

Quota Conversion

Conversion Rule

500,000 quota = $1.00 USD
Formula: USD amount = quota ÷ 500,000 Examples:
  • quota: 7500 → $0.015 USD
  • quota: 22500 → $0.045 USD
  • quota: 18 → $0.000036 USD
This is the same conversion used by the Balance Query API, so the two line up directly.

Error Responses

HTTP 401 - Authentication Failed

Reason: The system token is invalid or expired, or an API key (starting with sk-) was mistakenly used as a system token. Solution: Regenerate the system token in the console, and make sure Authorization carries the raw value without a Bearer prefix.

Code Examples

cURL Example (single page, quick check)

The --compressed option is required, because the API returns gzip-compressed content. Without it you will get garbled output.
This command returns at most 10 records. For real reconciliation, use the paginated version below.

Python Example (paginated, ready to run)

Sample output:

Node.js Example (paginated)

Both the Python requests library and the Node.js fetch API decompress gzip automatically, so no extra configuration is needed there. Only curl requires the explicit --compressed flag.

Common Scenarios

Calculating spend over a time range

Use the Python example above. The two things that matter are passing type=2 and dividing the summed quota by 500,000. To scope it to a single model, add the model_name parameter.

Finding failed calls

Requests rejected by the gateway (invalid parameters and similar) have quota of 0 and are not charged. The error_code in the log lets you separate “the call failed” from “the call succeeded but I did not like the result.”

Providing a request ID for support

Locate the problematic call in the logs and give support the request_id. That identifies the exact request end to end, which is far more efficient than describing “a call to some model failed around a certain time.”

FAQ

The server-side cap on page_size is 10. Larger values do not error but do not take effect either. To retrieve more you must paginate (p=0, p=1, p=2 and so on) until the response returns an empty array. The Python and Node.js examples above already encapsulate this.
No. Certain fields hold platform-internal information and are empty or zero from a regular account’s perspective. This is expected and does not affect the fields you need for reconciliation or troubleshooting — quota, model_name, error_code, and request_id are all fully populated.
The API returns the group identifier, while the console displays the group’s label. These can differ — for example the API returns default while the console shows Default.The full mapping is available from the public endpoint https://api.apiyi.com/api/pricing under the usable_group field, which maps identifier to label. If you want your reports to match the console, apply that mapping yourself.
Use quota. It is the amount actually deducted for the call and the only field suitable for reconciliation. For per-call priced models such as image and video generation, the token counts in the response may be placeholder values that do not participate in pricing — those models report by_count in other.billing_type.
Specify any range with start_timestamp and end_timestamp. For the exact retention period of historical data, please contact support. We recommend exporting reconciliation data periodically rather than relying on the API for long-term lookback.
Reason: the API returns gzip-compressed content (Content-Encoding: gzip) and curl is not decompressing it.Solution: add the --compressed flag:
The Python requests library and the Node.js fetch API decompress automatically.
No. The log query endpoint does not consume any quota.

Important Notes

A system token is not an API key, and the two are not interchangeable
  • An API key (starting with sk-) is for /v1/* inference endpoints. Using it against /api/log/self returns 401.
  • A system token (a plain string with no prefix) is for /api/* management endpoints. Using it against /v1/chat/completions returns an invalid-token error.
A system token’s scope covers your entire account, so treat it like your account password: store it in a secret manager rather than in code, never commit it to a repository, and rotate it periodically.
Log responses contain your own API keys in plain textEach log record carries information about the token that made the call. Do not paste raw log responses into public places, share screenshots of them, or hand them to third parties — strip sensitive fields before exporting.Note in particular that this plaintext does not carry the sk- prefix, so common secret scanners may not detect it. Do not rely on automated checks to catch it for you.
Request limits
  • Keep at least 1 second between queries to avoid rate limiting
  • Set a reasonable request timeout (30 seconds recommended)
  • For wide time ranges, implement pagination and retry handling

Related Documentation