> ## Documentation Index
> Fetch the complete documentation index at: https://docs.arouter.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# ユーザー追跡

> API リクエストの user フィールドを使用して、アプリケーションの個々のユーザーの使用状況を追跡します。ユーザーごとの分析、コスト配賦、不正利用検出に役立ちます。

ARouter は API リクエストに `user` フィールドを渡すことで、ユーザーごとの使用状況追跡をサポートします。これにより、アプリケーション内の個々のユーザーに API コストを帰属させ、ユーザーレベルで使用パターンを監視できます。

## ユーザー ID の渡し方

任意のチャット補完リクエストに `user` フィールドを含めます：

```json theme={null}
{
  "model": "openai/gpt-5.4",
  "messages": [{"role": "user", "content": "Hello!"}],
  "user": "user_12345"
}
```

`user` 値は、システム内のエンドユーザーを識別する任意の文字列にできます — データベース ID、ハッシュされたメールアドレス、またはセッション ID などです。ARouter はこの値を検証または解釈しません。

<Tabs>
  <Tab title="TypeScript">
    ```typescript theme={null}
    import OpenAI from "openai";

    const client = new OpenAI({
      baseURL: "https://api.arouter.ai/v1",
      apiKey: "lr_live_xxxx",
    });

    async function chatWithUser(userId: string, message: string) {
      const response = await client.chat.completions.create({
        model: "openai/gpt-5.4",
        messages: [{ role: "user", content: message }],
        user: userId,
      });
      return response.choices[0].message.content;
    }
    ```
  </Tab>

  <Tab title="Python">
    ```python theme={null}
    from openai import OpenAI

    client = OpenAI(
        base_url="https://api.arouter.ai/v1",
        api_key="lr_live_xxxx",
    )

    def chat_with_user(user_id: str, message: str) -> str:
        response = client.chat.completions.create(
            model="openai/gpt-5.4",
            messages=[{"role": "user", "content": message}],
            user=user_id,
        )
        return response.choices[0].message.content
    ```
  </Tab>

  <Tab title="cURL">
    ```bash theme={null}
    curl https://api.arouter.ai/v1/chat/completions \
      -H "Authorization: Bearer lr_live_xxxx" \
      -H "Content-Type: application/json" \
      -d '{
        "model": "openai/gpt-5.4",
        "messages": [{"role": "user", "content": "Hello!"}],
        "user": "user_12345"
      }'
    ```
  </Tab>
</Tabs>

***

## プライバシーに関する考慮事項

<Warning>
  個人識別情報（PII）を `user` 値として渡さないでください。ハッシュ化された ID やデータベースの主キーなど、不透明な識別子を使用してください。
</Warning>

推奨されるアプローチ：

```python theme={null}
import hashlib

# 渡す前にユーザーのメールアドレスをハッシュ化
user_id = hashlib.sha256(user_email.encode()).hexdigest()[:16]

# または不透明なデータベース ID を使用
user_id = str(user.id)  # 例：「usr_abc123」
```

***

## ユーザー別の使用状況を確認する

ユーザーレベルの使用データは [アクティビティエクスポート](/jp/guides/administration/activity-export) で確認できます。各レコードには渡した `user` フィールドが含まれており、以下のことができます：

* ユーザーごとの Token 消費とコストを計算
* 高使用量ユーザーを特定してキャパシティプランニング
* 異常な使用パターンや不正利用を検出
* エンドユーザー向けの使用量ベースの課金を構築

### アクティビティエクスポート API

ユーザーでフィルタリングして使用記録を照会：

```bash theme={null}
curl "https://api.arouter.ai/v1/activity?user=user_12345&limit=100" \
  -H "Authorization: Bearer lr_live_xxxx"
```

***

## ユースケース

### ユーザーごとのコスト配賦

各顧客へのサービス提供にかかるコストを追跡：

```typescript theme={null}
// 顧客 ID を渡す
const response = await client.chat.completions.create({
  model: "anthropic/claude-sonnet-4.6",
  messages: conversation,
  user: `customer_${customerId}`,
});

// 後で：アクティビティエクスポートを照会して顧客ごとの総コストを取得
```

### 不正利用検出

ユーザーごとに異常な使用パターンを監視：

```python theme={null}
# 各ユーザーに独自の識別子を付与
response = client.chat.completions.create(
    model="openai/gpt-5.4",
    messages=[{"role": "user", "content": user_message}],
    user=f"app_user_{user.id}",
)
```

### マルチテナントアプリケーション

マルチテナント SaaS アプリケーションでは、組織またはテナント ID を渡します：

```typescript theme={null}
const response = await client.chat.completions.create({
  model: "google/gemini-2.5-flash",
  messages: messages,
  user: `org_${organizationId}`,
});
```

***

## 注意事項

* `user` フィールドはオプションです。このフィールドを含まないリクエストも、API キーの集計使用量として追跡されます。
* ユーザー ID は不透明な文字列として保存されます。ARouter はその形式を検証、解析、または解釈しません。
* ユーザー追跡データは [アクティビティエクスポート](/jp/guides/administration/activity-export) レコードに含まれます。
* チームレベルの使用コントロールについては [組織管理](/jp/guides/administration/organization-management) を参照してください。
