Ch5. agent.ts, Models, Compaction
Interpret defineAgent configuration as runtime policy for models, output schemas, compaction, and experimental flags.
Written and reviewed by Eric Han · Co-founder & CTO, reopt
Key takeaways
agent.tsis not just a model picker; it is runtime policy and output contract.outputSchema, compaction, andmodelOptionsstrongly affect long-session quality and cost.- Experimental flags and hosted-build packaging controls need owners and release gates.
Usage limits in 0.82.0: sessionUsage
Replace maxInputTokensPerSession, maxOutputTokensPerSession, and maxTokenCostUsdPerSession with limits.sessionUsage. Returning true stops the next model call.
import { defineAgent } from "eve";
export default defineAgent({
model: "anthropic/claude-sonnet-5.5",
limits: {
sessionUsage: ({ window }) =>
window.inputTokens - window.cacheReadTokens >= 200_000 ||
window.outputTokens >= 20_000 ||
(window.costUsd ?? 0) >= 1.5,
},
});totalis cumulative;windowstarts after the last approved Continue. Continue resets only the window.- Input includes cache usage. Subtract cacheReadTokens for new input. Optional costUsd is Gateway-reported model token cost; it excludes tools and infrastructure.
- The call crossing a threshold may finish. These thresholds are not exact billing caps; use external spend controls as well.
- Interactive runs pause at
turn.waitingfor Continue/Stop. Stop cancels the active turn and delegation tree, leaving the session resumable. Runs without human input fail withSESSION_USAGE_LIMIT_REACHED. - Children use their own policy or the root policy, not an allocated share of the parent's remaining budget. Test aggregation timing and cancellation exceptions.
agent/agent.ts configures runtime behavior. In small demos it often only selects a model. In enterprise systems it decides model routing, output contracts, long-session memory behavior, and unstable feature adoption.
Basic Form
import { defineAgent } from "eve";
export default defineAgent({
model: "anthropic/claude-sonnet-5.5",
});The official agent.ts docs clarify two important rules:
| Rule | Operational meaning |
|---|---|
Root agent.ts can be omitted when no runtime config is needed | Useful for tiny agents, but production agents should pin model/config for review. |
If agent.ts is present, model is required | Do not hide policy behind an empty defineAgent({}). |
Model IDs below are routing examples. Pin the model and options you have evaluated; framework and scaffold defaults can change.
Model Routing Strategy
| Model definition | Example | Advantage | Caution |
|---|---|---|---|
| Gateway model id string | "anthropic/claude-opus-5.5" | Vercel AI Gateway routing, central policy, OIDC flow | Review Gateway model catalog and policy. |
AI SDK LanguageModel | anthropic("claude-opus-5.5") | Direct provider control | Install provider package and manage provider key/terms. |
For enterprise teams, Gateway-routed models are usually the default because usage, fallback, cost, and policy can be centralized. Direct provider models are still valid when specific provider options or self-host constraints require them.
Model Selection Matrix
| Agent type | Selection criteria |
|---|---|
| customer support | low latency, stable instruction following, safety behavior |
| internal research | long context, tool reasoning, citation discipline |
| code/file work | tool-call stability, sandbox behavior, large context |
| back-office automation | structured output reliability and approval UX |
| eval judge | consistency, cost, and separation from agent-under-test |
The goal is not "the smartest model." The goal is the model with the lowest failure cost for the capability surface.
modelOptions
Current modelOptions accepts providerOptions and promptCache. Set portable reasoning effort with the separate reasoning field; use modelOptions.providerOptions for provider-specific controls.
Operating rules:
- separate reasoning-heavy agents from transactional agents
- gate model option changes like prompt changes
- record model id and options in eval results
- document provider-specific portability risks
outputSchema
The 0.82.0 defineAgent type has no outputSchema field. Request structured output when creating a client session, sending a turn, or sending to a child session from a workflow. The schema applies only to that turn. A tool's own outputSchema is a separate return-value validation contract.
import { Client } from "eve/client";
import { z } from "zod";
const riskSchema = z.object({
risk: z.enum(["low", "medium", "high"]),
requiredApprovals: z.array(z.string()),
rationale: z.string(),
});
type RiskReview = z.infer<typeof riskSchema>;
const client = new Client({ host: "http://127.0.0.1:2000" });
const { response } = await client.sessions.create<RiskReview>({
message: "Review the proposed operation.",
outputSchema: riskSchema,
});
const result = await response.result();
if (!result.data) throw new Error("No structured result was produced.");
console.log(result.data.risk);The server validates the schema. A client generic does not independently revalidate the payload, and data may be undefined when no structured result was produced. Configure route authentication before using a remote host.
Compaction
The default harness summarizes older turns as the context window fills. The default threshold is 90%.
export default defineAgent({
model: "anthropic/claude-opus-5.5",
compaction: {
thresholdPercent: 0.75,
},
});Compaction saves context and cost, but it is also a risk surface. A bad summary can corrupt the factual base of a long session.
| Workload | Recommended policy |
|---|---|
| short FAQ/support | default threshold |
| long research | compact earlier; persist key artifacts in files/state |
| coding/file work | read-before-write evidence can disappear from summary |
| regulated decisioning | externalize important evidence |
| multi-subagent work | structure child results before parent aggregation |
Experimental Flags
Do not copy the old experimental.codeMode or ExperimentalWorkflow exports into current configuration. Model-generated orchestration now uses the lowercase workflow() factory from eve/tools/workflow. Authored durable work uses defineWorkflowTool with a "use workflow" directive.
Check experimental.workflow world, retention, and checkpoint-batching settings against current types and a compatible World version. Keep replayed workflow bodies separate from steps that perform external effects, and configure approval independently.
Workflow tools · Agent config.
Hosted Build Packaging
build.externalDependencies keeps selected packages external in hosted output. It is packaging control, not security approval.
Review:
- package install availability in target host
- third-party SDK network and data behavior
- security review for externalized dependency
- parity between Vercel and self-host environments
Review Checklist
| Item | Question |
|---|---|
| model | Is the routing path approved? |
| modelOptions | Are behavior and cost intended? |
| outputSchema | Is the schema on the intended turn rather than defineAgent? |
| compaction | Can the workload tolerate summary drift? |
| experimental | Are unstable features scoped and evaluated? |
| build | Are external dependencies reviewed and deployable? |