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

# Sitzungen und Richtlinien

> Verfolgung von Mehrturnsitzungen, Durchsetzung von Richtlinien und Middleware für das RAIL JavaScript SDK.

<Info>
  **Python:** [Sitzungen](/sdk/python/sessions) | [Richtlinien-Engine](/sdk/python/policy-engine) | **Konzepte:** [Richtlinien-Engine](/concepts/policy-engine)
</Info>

## Sitzungsverfolgung

`RAILSession` verfolgt Punktzahlen über ein Mehrturngespräch mit konfigurierbarer Frequenz für tiefgehende Auswertungen und Qualitätsgrenzwerte.

```typescript theme={null}
import { RAILSession } from '@responsible-ai-labs/rail-score';

const session = new RAILSession(client, {
  deepEvalFrequency: 5,    // Tiefenauswertung alle 5 Turns
  contextWindow: 10,        // Letzte 10 Turns verfolgen
  qualityThreshold: 7.0,    // Tiefenauswertung auslösen, wenn die Punktzahl darunter fällt
});

// Füge einen Gesprächs-Turn hinzu
const result = await session.addTurn("Inhalt der AI-Antwort");
console.log(result.rail_score.score);

// Sitzungsmetriken
const metrics = session.getMetrics();
console.log(`Durchschnitt: ${metrics.averageScore}`);
console.log(`Min: ${metrics.minScore}, Max: ${metrics.maxScore}`);
console.log(`Bestehensquote: ${metrics.passingRate}`);
console.log(`Turns: ${metrics.turnCount}`);

for (const [dim, avg] of Object.entries(metrics.dimensionAverages)) {
  console.log(`  ${dim}: ${avg.toFixed(1)}`);
}

// Zurücksetzen für neues Gespräch
session.reset();
```

## Richtlinien-Engine

Durchsetzen von Inhaltsqualitätsrichtlinien mit vier Modi: `LOG_ONLY`, `BLOCK`, `REGENERATE`, `CUSTOM`.

```typescript theme={null}
import { PolicyEngine, RAILBlockedError } from '@responsible-ai-labs/rail-score';

const policy = new PolicyEngine(client, {
  mode: "BLOCK",
  thresholds: { safety: 7.0, privacy: 7.0 },
});

try {
  const result = await policy.enforce("Inhalt zur Überprüfung");
  console.log(result.evaluation.rail_score.score);
  console.log(result.passed);             // true/false
  console.log(result.failedDimensions);   // ["safety"] oder []
} catch (error) {
  if (error instanceof RAILBlockedError) {
    console.log(`Blockiert: ${error.message}`);
  }
}

// Laufzeit-Neu-Konfiguration
policy.setMode("LOG_ONLY");
policy.setThresholds({ safety: 8.0 });

// CUSTOM-Modus — Ihre eigene Durchsetzungslogik
const customPolicy = new PolicyEngine(client, {
  mode: "CUSTOM",
  thresholds: { safety: 7.0 },
  customCallback: async (content, evalResult) => {
    return `[Überprüft] ${content}`;
  },
});
```

## Middleware

Umwickeln Sie jede asynchrone Funktion mit vor/nach RAIL-Auswertung:

```typescript theme={null}
import { RAILMiddleware } from '@responsible-ai-labs/rail-score';

const middleware = new RAILMiddleware(client, {
  inputThresholds:  { safety: 5.0 },
  outputThresholds: { safety: 7.0, privacy: 7.0 },
  onInputEval:  (result) => console.log(`Eingabepunktzahl: ${result.rail_score.score}`),
  onOutputEval: (result) => console.log(`Ausgabepunktzahl: ${result.rail_score.score}`),
});

const safeLLMCall = middleware.wrap(async (input) => {
  return await myLLM.generate(input);
});

const output = await safeLLMCall("Benutzernachricht");
```

## Was kommt als Nächstes

<CardGroup cols={2}>
  <Card title="Anbieter und Referenz" icon="plug" href="/sdk/javascript/providers">
    LLM-Anbieter-Wrapper, Fehlerbehandlung und TypeScript-Typen.
  </Card>

  <Card title="Konzepte: Richtlinien-Engine" icon="gavel" href="/concepts/policy-engine">
    Wie Richtlinienregeln im Hintergrund funktionieren.
  </Card>
</CardGroup>
