# `PhoenixKit.Modules.AI.Request`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L1)

AI request schema for PhoenixKit AI system.

Tracks every AI API request for usage history and statistics.
Used for monitoring costs, performance, and debugging.

## Schema Fields

### Request Identity
- `endpoint_uuid`: Foreign key to the AI endpoint used
- `endpoint_name`: Denormalized endpoint name for historical display
- `prompt_uuid`: Foreign key to the AI prompt used (if request used a prompt template)
- `prompt_name`: Denormalized prompt name for historical display
- `user_uuid`: Foreign key to the user who made the request (nullable if user deleted)
- `slot_index`: Which slot was used (deprecated, for backward compatibility)

### Request Details
- `model`: Model identifier (e.g., "anthropic/claude-3-haiku")
- `request_type`: Type of request (e.g., "text_completion", "chat")

### Token Usage
- `input_tokens`: Number of tokens in the prompt
- `output_tokens`: Number of tokens in the response
- `total_tokens`: Total tokens used (input + output)

### Performance & Cost
- `cost_cents`: Estimated cost in nanodollars (when available)
- `latency_ms`: Response time in milliseconds
- `status`: Request status - "success", "error", or "timeout"
- `error_message`: Error details if status is not "success"

### Metadata
- `metadata`: Additional context (temperature, max_tokens, etc.)

## Status Types

- `success` - Request completed successfully
- `error` - Request failed with an error
- `timeout` - Request timed out

## Usage Examples

    # Log a successful request
    {:ok, request} = PhoenixKit.Modules.AI.create_request(%{
      endpoint_uuid: endpoint.uuid,
      endpoint_name: "Claude Fast",
      user_uuid: user.uuid,
      model: "anthropic/claude-3-haiku",
      request_type: "chat",
      input_tokens: 150,
      output_tokens: 320,
      total_tokens: 470,
      latency_ms: 850,
      status: "success",
      metadata: %{"temperature" => 0.7}
    })

    # Log a failed request
    {:ok, request} = PhoenixKit.Modules.AI.create_request(%{
      endpoint_uuid: endpoint.uuid,
      endpoint_name: "Claude Fast",
      model: "anthropic/claude-3-haiku",
      status: "error",
      error_message: "Rate limit exceeded"
    })

# `changeset`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L130)

Creates a changeset for request creation.

# `format_cost`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L215)

Formats the cost for display.

Cost is stored in nanodollars (1/1000000 of a dollar) for precision.
Shows appropriate precision based on the amount:
- >= $1.00: 2 decimal places ($1.23)
- >= $0.01: 2 decimal places ($0.05)
- >= $0.0001: 4 decimal places ($0.0012)
- > $0: 6 decimal places ($0.000030)

# `format_latency`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L192)

Formats the latency for display.

# `format_tokens`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L199)

Formats the token count for display.

# `short_model_name`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L233)

Extracts the model name without provider prefix.

# `status_color`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L184)

Returns a CSS class for the status badge.

# `status_label`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L176)

Returns a human-readable status label.

# `valid_request_types`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L171)

Returns the list of valid request types.

# `valid_statuses`
[🔗](https://github.com/BeamLabEU/phoenix_kit/blob/v1.7.71/lib/modules/ai/request.ex#L166)

Returns the list of valid status types.

---

*Consult [api-reference.md](api-reference.md) for complete listing*
