LunaKI-StückkostenEin Produkt von Luna · Azure-Kosten pro erfolgreichem Auftrag

Lese-Skript (nur lesen)

Das optionale PowerShell-Leseskript nutzt die bereits angemeldete Azure-PowerShell-Sitzung des Kunden und liest nur Token-Tageswerte auf Ressourcenebene aus Azure Monitor (Az.Accounts, Az.Monitor; ausschließlich Get-*). Es schreibt genau eine lokale JSON-Datei token-nutzung.resource.v1.json mit pseudonymisierter Ressourcenkennung für die lokale App-Zuordnung. Keine Anmeldung an einer Hersteller-App, kein Upload, keine Azure-Schreiboperation.

Skript herunterladen Anleitung

<#
  KI-Stückkosten – Optionales Lese-Skript für Token-Tageswerte auf Ressourcenebene
  Dubai AI şirketimiz group L.L.C-FZ, Dubai - https://ai-stueckkosten.lunafzai.com
  Version 1.0

  Was es tut:
    Nutzt die bereits angemeldete Azure-PowerShell-Sitzung des Kunden und liest aus Azure Monitor
    die Tageswerte der Metriken ProcessedPromptTokens und GeneratedTokens fuer GENAU EINE Azure-Ressource.
    Es schreibt GENAU EINE lokale JSON-Datei.

  Was es NICHT tut:
    Keine Anmeldung an einer Hersteller-App, kein Upload, keine Azure-Schreiboperation,
    keine Graph-, Exchange-, SharePoint- oder Microsoft-365-Abfragen.

  Voraussetzungen:
    PowerShell 7, Module Az.Accounts und Az.Monitor.
    Azure RBAC Monitoring Reader auf der Zielressource oder gleichwertige Leserolle.

  Beispiel:
    .\ki-stueckkosten-token-leseprobe.ps1 -ResourceId "/subscriptions/000.../resourceGroups/rg/providers/Microsoft.CognitiveServices/accounts/aoai-demo" -StartUtc "2026-11-01T00:00:00Z" -EndUtc "2026-12-01T00:00:00Z"
#>
param(
  [Parameter(Mandatory)][string]$ResourceId,
  [Parameter(Mandatory)][datetime]$StartUtc,
  [Parameter(Mandatory)][datetime]$EndUtc,
  [string]$Ausgabe = ".\token-nutzung.resource.v1.json"
)
$ErrorActionPreference = 'Stop'
if ($EndUtc -le $StartUtc) { throw 'EndUtc muss strikt nach StartUtc liegen.' }

Import-Module Az.Accounts -ErrorAction Stop
Import-Module Az.Monitor -ErrorAction Stop
$ctx = Get-AzContext
if (-not $ctx) { throw 'Keine aktive Azure-PowerShell-Sitzung gefunden. Bitte zuerst mit Connect-AzAccount anmelden.' }

function Get-PseudoId([string]$Text) {
  $sha = [System.Security.Cryptography.SHA256]::Create()
  try {
    $bytes = [System.Text.Encoding]::UTF8.GetBytes($Text.ToLowerInvariant())
    $hash = $sha.ComputeHash($bytes)
    return ('rid_' + ([BitConverter]::ToString($hash[0..7]).Replace('-', '').ToLowerInvariant()))
  }
  finally {
    $sha.Dispose()
  }
}

$warnings = New-Object System.Collections.Generic.List[string]
$defs = @(Get-AzMetricDefinition -ResourceId $ResourceId)
$defNames = @{}
foreach ($d in $defs) { if ($d.Name -and $d.Name.Value) { $defNames[$d.Name.Value] = $true } }
$wanted = @('ProcessedPromptTokens', 'GeneratedTokens')
foreach ($m in $wanted) {
  if (-not $defNames.ContainsKey($m)) { [void]$warnings.Add("Metrik nicht verfuegbar: $m") }
}

function Read-MetricDaily([string]$MetricName) {
  if (-not $defNames.ContainsKey($MetricName)) { return @() }
  $res = Get-AzMetric -ResourceId $ResourceId -MetricName $MetricName -StartTime $StartUtc.ToUniversalTime() -EndTime $EndUtc.ToUniversalTime() -TimeGrain ([TimeSpan]::FromDays(1)) -AggregationType Total -DetailedOutput
  $out = @()
  foreach ($item in @($res.Data)) {
    if (-not $item.TimeStamp) { continue }
    $v = $item.Total
    if ($null -eq $v) { continue }
    $out += [pscustomobject]@{
      date = ([datetime]$item.TimeStamp).ToUniversalTime().ToString('yyyy-MM-dd')
      value = [double]$v
    }
  }
  return $out
}

$pp = Read-MetricDaily -MetricName 'ProcessedPromptTokens'
$gt = Read-MetricDaily -MetricName 'GeneratedTokens'
$byDate = @{}
foreach ($r in $pp) {
  if (-not $byDate.ContainsKey($r.date)) {
    $byDate[$r.date] = [ordered]@{ date = $r.date; processed_prompt_tokens = $null; generated_tokens = $null }
  }
  $byDate[$r.date].processed_prompt_tokens = $r.value
}
foreach ($r in $gt) {
  if (-not $byDate.ContainsKey($r.date)) {
    $byDate[$r.date] = [ordered]@{ date = $r.date; processed_prompt_tokens = $null; generated_tokens = $null }
  }
  $byDate[$r.date].generated_tokens = $r.value
}

$rows = @($byDate.Keys | Sort-Object | ForEach-Object { [pscustomobject]$byDate[$_] })
$result = [ordered]@{
  schema_version = 1
  query_scope = 'resource'
  resource_id = $ResourceId
  resource_pseudo_id = (Get-PseudoId -Text $ResourceId)
  start_utc = $StartUtc.ToUniversalTime().ToString('o')
  end_utc_exclusive = $EndUtc.ToUniversalTime().ToString('o')
  grain = 'P1D'
  aggregation = 'Total'
  rows = $rows
  warnings = @($warnings)
}

$json = $result | ConvertTo-Json -Depth 6
[System.IO.File]::WriteAllText((Resolve-Path -LiteralPath (Split-Path -Parent $Ausgabe) -ErrorAction SilentlyContinue | ForEach-Object { $_.Path } | Select-Object -First 1) ? $Ausgabe : $Ausgabe, $json, [System.Text.UTF8Encoding]::new($false))
Write-Host "OK: $Ausgabe" -ForegroundColor Green