Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 10 additions & 3 deletions app/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,16 @@ browser (React + useAgentChat over WebSocket)
endpoint. Connections use the MCP v2 client (`@modelcontextprotocol/client`
2.x, stateless streamable HTTP). Identity travels as `x-user-netid` headers
set per connection; changing netid reconnects everything.
- **Models**: Claude Opus 5 by default, with Sonnet 5 and a Workers AI
fallback ("Campus") in the switcher. Claude models require the
`ANTHROPIC_API_KEY` secret; without it every choice falls back to Campus.
- **Models**: Claude Sonnet 5.5 by default, with Haiku 5.5 and Opus 5 in the
switcher. Existing Opus choices are preserved. Saved Sonnet 5 and retired
model choices resolve to Sonnet 5.5. All models require `ANTHROPIC_API_KEY`.
Prompt caching marks the last system block (covering tool definitions) and
the last conversation block with the default five-minute cache TTL.
Reuse requires an unchanged prefix, the same model, and enough prompt tokens.
Check `cacheRead` in `pi.usage` after a follow-up step or turn to verify a hit.
The weekly budget restricts Opus to Sonnet 5.5 after tier one and preserves
Haiku choices. Haiku usage is priced per request, including its higher rates
above 100,000 prompt tokens, before costs are summed for the turn.
- **Identity**: Princeton CAS sign-in (`src/server/auth.ts`, using
`https://authenticate.princeton.edu/cas`). The Worker validates service
tickets through `/p3/serviceValidate` over HTTPS and takes the NetID from
Expand Down
9 changes: 4 additions & 5 deletions app/src/client/lib/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ import { useSyncExternalStore } from "react";
import {
COVERED_BY_JUNCTION,
DEFAULT_APPS,
PI_MODELS,
DEFAULT_MODEL,
resolveModel,
type AppKey,
type PiModel,
type PiSettings,
Expand Down Expand Up @@ -33,7 +34,7 @@ export const GENERAL_CHAT_TITLE = "PI general chat";
/** User-tweakable preferences; identity is never stored here. */
export type Prefs = { apps: AppKey[]; model: PiModel };

const DEFAULT_PREFS: Prefs = { apps: DEFAULT_APPS, model: "claude-opus-5" };
const DEFAULT_PREFS: Prefs = { apps: DEFAULT_APPS, model: DEFAULT_MODEL };

/**
* Stamped on every saved blob, so a one-time migration runs once. Bump it only
Expand Down Expand Up @@ -96,9 +97,7 @@ function getPrefs(netid: string): Prefs {
const stored: StoredPrefs = raw ? JSON.parse(raw) : {};
let merged: Prefs = { ...DEFAULT_PREFS, ...stored };
// A retired model choice (e.g. the old "campus") falls back to default.
if (!PI_MODELS.some((m) => m.value === merged.model)) {
merged.model = DEFAULT_PREFS.model;
}
merged.model = resolveModel(merged.model);
if (raw) {
merged = migrate(stored, merged);
// Written back straight away, stamp and all: a migration that reruns
Expand Down
2 changes: 1 addition & 1 deletion app/src/client/pages/ChatPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ function ChatBody({
// a consent card must not call it on until it has.
const [engineNote, setEngineNote] = useState<string | null>(null);
const [gcalReady, setGcalReady] = useState(false);
/** Why answers changed: Sonnet only, or nothing left this week. */
/** Why answers changed: restricted model choices, or nothing left this week. */
const [usageNote, setUsageNote] = useState<string | null>(null);
const agent = useAgent({
agent: "pi",
Expand Down
32 changes: 16 additions & 16 deletions app/src/server/pi.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@ import { createAnthropic } from "@ai-sdk/anthropic";
import { Think } from "@cloudflare/think";
import { callable, getAgentByName } from "agents";
import type { AgentMcpOAuthProvider } from "agents/mcp/do-oauth-client-provider";
import { hasToolCall, tool, type UIMessage } from "ai";
import { hasToolCall, tool, wrapLanguageModel, type UIMessage } from "ai";
import { z } from "zod";
import { EMPTY_WORKSPACES, workspaceContext, type WorkspaceState } from "../shared/workspaces";
import { handleWorkspaceRequest, WORKSPACE_KEY } from "./workspaces";
import {
TIER2_MODEL,
budgetFrom,
limitNote,
modelForBudget,
usageStatus,
type Budget,
type Spend,
type UsageStatus,
} from "../shared/usage-limit";
import { NOTICE_MODEL_ID, noticeModel } from "./notice-model";
import { promptCache } from "./prompt-cache";
import {
addStep,
costUsd,
Expand All @@ -33,10 +34,13 @@ import {
import {
COVERED_BY_JUNCTION,
DEFAULT_ENGINE_BASE,
DEFAULT_MODEL,
PI_APPS,
resolveModel,
toolOwner,
type AppKey,
type PiApp,
type PiModel,
type PiSettings,
} from "../shared/apps";

Expand All @@ -59,9 +63,6 @@ export type PiState = {
usage?: UsageStatus;
};

const CLAUDE_MODELS = new Set(["claude-opus-5", "claude-sonnet-5"]);
const DEFAULT_CLAUDE_MODEL = "claude-opus-5";

/** The apps PI may ask a student to switch on. */
const APP_KEYS = PI_APPS.map((a) => a.key) as [AppKey, ...AppKey[]];

Expand Down Expand Up @@ -238,7 +239,7 @@ export class Pi extends Think<Env, PiState> {
private turnUsage: TurnUsage | null = null;
private turnStartedAt = 0;
/** The model getModel() resolved, for pricing the turn. */
private turnModel = DEFAULT_CLAUDE_MODEL;
private turnModel: string = DEFAULT_MODEL;

getDefaultTimezone() {
return "America/New_York";
Expand All @@ -251,10 +252,8 @@ export class Pi extends Think<Env, PiState> {
}

/** The Claude model the student picked in the composer. */
private selectedModel(): string {
const preference = this.getConfig<PiSettings>()?.model ?? DEFAULT_CLAUDE_MODEL;
// Stale configs may still carry the retired "campus" preference.
return CLAUDE_MODELS.has(preference) ? preference : DEFAULT_CLAUDE_MODEL;
private selectedModel(): PiModel {
return resolveModel(this.getConfig<PiSettings>()?.model);
}

private claude(model: string) {
Expand All @@ -263,7 +262,9 @@ export class Pi extends Think<Env, PiState> {
// No silent fallback model: fail the turn loudly so ops notices.
throw new Error("ANTHROPIC_API_KEY is not configured");
}
return createAnthropic({ apiKey: key })(model);
// Every Claude call goes through here, so this is where prompt caching
// lands: both the composer's model and the budget downgrade.
return wrapLanguageModel({ model: createAnthropic({ apiKey: key })(model), middleware: promptCache });
}

getSystemPrompt() {
Expand Down Expand Up @@ -571,11 +572,10 @@ export class Pi extends Think<Env, PiState> {
maxSteps: 1,
};
}
// Past the Opus budget, every answer comes from Sonnet whatever the
// composer says, the same way PrincetonCourses steps down.
// Tier two restricts Opus, but keeps a student's cheaper Haiku selection.
const selected = this.selectedModel();
const downgrade = usage?.tier === 2 && selected !== TIER2_MODEL;
this.turnModel = downgrade ? TIER2_MODEL : selected;
const allowed = modelForBudget(selected, usage);
this.turnModel = allowed;
const workspaceNote = await (await this.deskStub(settings.netid)).getWorkspaceContext(
this.name.slice(`u-${settings.netid}-`.length)
);
Expand Down Expand Up @@ -640,7 +640,7 @@ export class Pi extends Think<Env, PiState> {
: [];
return {
...(inherited ?? {}),
...(downgrade ? { model: this.claude(TIER2_MODEL) } : {}),
...(allowed !== selected ? { model: this.claude(allowed) } : {}),
// The system prompt is assembled before this hook runs, so a turn that
// lost an app can only correct the record here — otherwise the model
// reads "Connected TigerApps: TigerJunction" and answers with the
Expand Down
74 changes: 74 additions & 0 deletions app/src/server/prompt-cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import type { LanguageModelMiddleware } from "ai";

/**
* Anthropic prompt caching for every Claude call PI makes.
*
* Each step of a turn resends the tool definitions, the system prompt and the
* whole conversation. Without cache breakpoints Anthropic bills all of it at
* the full input price every time (measured 2026-10-29: zero cache reads on
* the production key). Two breakpoints fix that:
*
* 1. The last system message. Anthropic caches the prefix up to a breakpoint,
* and the prefix is tools, then system, then messages, so this one covers
* the tool definitions and the system prompt. Reused while the entire
* prefix is unchanged, including the student's identity and workspace.
* 2. The last message. The next step (tool result) or the next turn extends
* the conversation, so the previous breakpoint can become a cache read and the
* new last message takes over. Anthropic looks back up to 20 blocks for a
* matching breakpoint. Changes to earlier content or the model, expiry,
* and growth beyond that window can cause a miss.
*
* The default cache TTL is five minutes. Prompts under the model's minimum
* are silently not cached (512 tokens on Sonnet 5.5 and Haiku 5.5).
* Verify reuse through cacheRead in pi.usage rather than assuming a hit.
* https://platform.claude.com/docs/en/build-with-claude/prompt-caching
*/

type Params = Parameters<NonNullable<LanguageModelMiddleware["transformParams"]>>[0]["params"];
export type Prompt = Params["prompt"];
type Message = Prompt[number];

const EPHEMERAL = { cacheControl: { type: "ephemeral" } } as const;

/** True when the message already carries an Anthropic cache breakpoint, ours or anyone's. */
function hasBreakpoint(message: Message): boolean {
const anthropic = message.providerOptions?.anthropic as Record<string, unknown> | undefined;
return Boolean(anthropic && (anthropic.cacheControl || anthropic.cache_control));
}

function withBreakpoint(message: Message): Message {
return {
...message,
providerOptions: {
...(message.providerOptions ?? {}),
anthropic: { ...((message.providerOptions?.anthropic as Record<string, unknown>) ?? {}), ...EPHEMERAL },
},
} as Message;
}

/**
* The prompt with the two breakpoints above. Pure, so it can be tested
* without a model: never adds a breakpoint a message already has, never
* marks the same message twice, and leaves an empty prompt alone.
*/
export function withCacheBreakpoints(prompt: Prompt): Prompt {
if (prompt.length === 0) return prompt;
const out = [...prompt];
let lastSystem = -1;
for (let i = 0; i < out.length; i++) {
if (out[i].role === "system") lastSystem = i;
}
if (lastSystem >= 0 && !hasBreakpoint(out[lastSystem])) {
out[lastSystem] = withBreakpoint(out[lastSystem]);
}
const last = out.length - 1;
if (last !== lastSystem && !hasBreakpoint(out[last])) {
out[last] = withBreakpoint(out[last]);
}
return out;
}

/** Wrap an Anthropic model with `wrapLanguageModel({ model, middleware: promptCache })`. */
export const promptCache: LanguageModelMiddleware = {
transformParams: async ({ params }) => ({ ...params, prompt: withCacheBreakpoints(params.prompt) }),
};
44 changes: 35 additions & 9 deletions app/src/server/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
* the cost analysis reads (Workers Logs parses JSON lines into fields).
*/

/** USD per million tokens, from Anthropic's price list. */
/** USD per million tokens, verified 2026-11-05.
* https://platform.claude.com/docs/en/about-claude/pricing
*/
type Price = {
input: number;
output: number;
Expand All @@ -21,7 +23,8 @@ export const MODEL_PRICES: Record<string, Price> = {
"claude-opus-5": { input: 5, output: 25, cacheRead: 0.5, cacheWrite5m: 6.25, cacheWrite1h: 10 },
"claude-sonnet-5": { input: 2, output: 10, cacheRead: 0.2, cacheWrite5m: 2.5, cacheWrite1h: 4 },
"claude-opus-5-5": { input: 4, output: 20, cacheRead: 0.2, cacheWrite5m: 5, cacheWrite1h: 8 },
"claude-sonnet-5-5": { input: 2, output: 10, cacheRead: 0.2, cacheWrite5m: 2.5, cacheWrite1h: 4 },
"claude-sonnet-5-5": { input: 2, output: 10, cacheRead: 0.1, cacheWrite5m: 2.5, cacheWrite1h: 4 },
"claude-haiku-5-5": { input: 0.1, output: 0.5, cacheRead: 0.01, cacheWrite5m: 0.125, cacheWrite1h: 0.2 },
};

/** Anthropic's server-side web search: $10 per 1,000 searches. */
Expand All @@ -39,8 +42,18 @@ export type TurnUsage = {
output: number;
reasoning: number;
webSearches: number;
/** Tokens from individual requests above Haiku 5.5's 100k prompt threshold.
* Kept separately so a long step does not reprice earlier short steps.
*/
longContext?: TokenCounts;
};

const TOKEN_KINDS = ["input", "cacheRead", "cacheWrite5m", "cacheWrite1h", "output"] as const;
type TokenCounts = Pick<TurnUsage, (typeof TOKEN_KINDS)[number]>;

const tokenCost = (usage: TokenCounts, price: Price): number =>
TOKEN_KINDS.reduce((sum, kind) => sum + usage[kind] * price[kind], 0);

export function emptyTurnUsage(): TurnUsage {
return {
steps: 0,
Expand Down Expand Up @@ -91,15 +104,28 @@ export function addStep(total: TurnUsage, usage: StepUsage | undefined): TurnUsa
noCache !== undefined
? n(noCache)
: Math.max(0, n(usage?.inputTokens) - cacheRead - cacheWrite);
const output = n(usage?.outputTokens);
let longContext = total.longContext;
// The threshold counts all prompt tokens, including reads and writes.
if (input + cacheRead + cacheWrite > 100_000) {
longContext = {
input: (longContext?.input ?? 0) + input,
cacheRead: (longContext?.cacheRead ?? 0) + cacheRead,
cacheWrite5m: (longContext?.cacheWrite5m ?? 0) + cacheWrite - cacheWrite1h,
cacheWrite1h: (longContext?.cacheWrite1h ?? 0) + cacheWrite1h,
output: (longContext?.output ?? 0) + output,
};
}
return {
steps: total.steps + 1,
input: total.input + input,
cacheRead: total.cacheRead + cacheRead,
cacheWrite5m: total.cacheWrite5m + cacheWrite - cacheWrite1h,
cacheWrite1h: total.cacheWrite1h + cacheWrite1h,
output: total.output + n(usage?.outputTokens),
output: total.output + output,
reasoning: total.reasoning + n(usage?.outputTokenDetails?.reasoningTokens),
webSearches: total.webSearches + n(raw.server_tool_use?.web_search_requests),
...(longContext ? { longContext } : {}),
};
}

Expand All @@ -110,12 +136,12 @@ export function addStep(total: TurnUsage, usage: StepUsage | undefined): TurnUsa
export function costUsd(model: string, usage: TurnUsage): number | null {
const price = MODEL_PRICES[model];
if (!price) return null;
const tokens =
usage.input * price.input +
usage.cacheRead * price.cacheRead +
usage.cacheWrite5m * price.cacheWrite5m +
usage.cacheWrite1h * price.cacheWrite1h +
usage.output * price.output;
let tokens = tokenCost(usage, price);
// Haiku charges 5x the base token rates for each request above 100k.
// The base cost is already counted, so add only the remaining 4x.
if (model === "claude-haiku-5-5" && usage.longContext) {
tokens += 4 * tokenCost(usage.longContext, price);
}
return tokens / 1_000_000 + usage.webSearches * WEB_SEARCH_USD;
}

Expand Down
14 changes: 11 additions & 3 deletions app/src/shared/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -291,13 +291,21 @@ export function countConnected(
export const DEFAULT_ENGINE_BASE = "https://junction-engine.tigerapps.org";

/** Models the switcher offers. */
export type PiModel = "claude-opus-5" | "claude-sonnet-5";
export type PiModel = "claude-opus-5" | "claude-sonnet-5-5" | "claude-haiku-5-5";

export const DEFAULT_MODEL: PiModel = "claude-sonnet-5-5";

export const PI_MODELS: Array<{ value: PiModel; label: string }> = [
{ value: "claude-sonnet-5-5", label: "Sonnet 5.5, balanced" },
{ value: "claude-haiku-5-5", label: "Haiku 5.5, fastest" },
{ value: "claude-opus-5", label: "Opus 5, sharpest" },
{ value: "claude-sonnet-5", label: "Sonnet 5, quicker" },
];

/** Shared by persisted browser preferences and the server's model allowlist. */
export function resolveModel(preference: unknown): PiModel {
return PI_MODELS.find((model) => model.value === preference)?.value ?? DEFAULT_MODEL;
}

/** Settings the client pushes to a Pi agent instance. */
export type PiSettings = {
netid: string;
Expand All @@ -308,5 +316,5 @@ export type PiSettings = {
export const DEFAULT_SETTINGS: PiSettings = {
netid: "",
apps: DEFAULT_APPS,
model: "claude-opus-5",
model: DEFAULT_MODEL,
};
Loading
Loading