Agent Architecture
Fast Agent & Tool Routing with TypeSafe Jev: Subagent Dispatch, Model Cascades, and Action Gates
How to use TypeSafe Jev as a 100ms System 1 decision layer in agent systems for tool dispatch, subagent delegation, model routing, and safety guardrails.
Autonomous agent loops spend substantial time and money calling heavy frontier LLMs simply to decide what to do next—choosing which tool to invoke, selecting which specialist subagent to trigger, or determining whether a goal is satisfied.
Using an autoregressive generative model for routing adds sequential latency (often seconds per call) to every loop iteration. TypeSafe reports typical service latencies of 70ms to 500ms for Jev at $0.042 per million input tokens, allowing systems to reserve generative models exclusively for synthesis and code execution.
The Two-Tier Agent Architecture
In a standard agent architecture, a single generative LLM handles perception, routing, argument extraction, tool calling, and response synthesis in a continuous chat loop.
In a two-tier System 1 / System 2 architecture, Jev handles discrete control decisions, while the LLM handles open-ended text generation:
Standard Agent Loop:
State ──> [Frontier LLM (often seconds)] ──> Parse JSON Tool Call ──> Execute Tool ──> [Frontier LLM] ──> Finish
Two-Tier Jev-Accelerated Loop:
State ──> [Jev System 1 (70ms–500ms)] ──> Tool Selected & Confirmed
│
├── If tool requires no generative text ──> Execute Tool Immediately
└── If tool requires generative arguments ──> Generative LLM generates parameters
1. Tool Selection with Choice
When an agent needs to pick among a bounded set of registered tools, use Jev's Choice primitive. Define the tools as options and use criteria to document when each tool is appropriate:
from typesafe_sdk import TypeSafeClient, Choice
client = TypeSafeClient()
agent_state = {
"user_goal": "Check why our deploy failed on commit a9f81b",
"recent_actions": ["cloned_repo", "installed_dependencies"],
"working_directory": "/workspace/app"
}
response = client.system_one(
model="jev-latest",
state=agent_state,
questions={
"next_tool": Choice(
instructions="Select the next tool to run to advance toward 'user_goal'.",
criteria={
"git_log": "Inspect recent commit history or diffs",
"read_file": "Inspect configuration or source code files",
"run_command": "Execute terminal commands or test suites",
"web_search": "Search external documentation or error codes",
"finish_task": "Goal is completely satisfied"
}
)
}
)
selected = response.answers["next_tool"]
print(f"Tool: {selected.choice} (Confidence: {selected.confidence:.1%})")
# Example illustrative output: Tool: run_command (Confidence: 94.2%)
Because Jev returns a strict enum value, the agent loop never crashes on invalid tool names or malformed tool call JSON blocks.
2. Model Routing Cascades
Not every user query requires a heavy reasoning model. Running an expensive model on simple lookups wastes budget and adds latency.
You can use Jev as an intake router to direct tasks across model tiers:
response = client.system_one(
model="jev-latest",
state={"prompt": user_prompt},
questions={
"model_tier": Choice(
instructions="Route prompt to the most cost-effective model capable of answering accurately.",
criteria={
"fast_tier": "Factual queries, simple formatting, small summaries, or keyword searches",
"standard_tier": "Multi-paragraph writing, standard code generation, or common debugging",
"reasoning_tier": "Complex architectural design, deep mathematical logic, or multi-file refactors"
}
)
}
)
tier = response.answers["model_tier"].choice
if tier == "fast_tier":
call_fast_model(user_prompt) # Fast, lower-cost model
elif tier == "standard_tier":
call_standard_model(user_prompt) # General-purpose standard model
else:
call_reasoning_model(user_prompt) # Frontier reasoning model
Because Jev input tokens cost $0.042 per million, routing common requests to faster or lower-cost models can significantly reduce aggregate spend compared to sending every prompt directly to a frontier reasoning model.
3. Subagent Delegation in Multi-Agent Graphs
In hierarchical multi-agent frameworks, an orchestrator agent often delegates tasks to specialized subagents (such as a CodebaseResearcher, DatabaseDebugger, or ReviewerAgent).
Instead of prompting an LLM orchestrator to write a delegation message, Jev evaluates the state and assigns the task directly:
import { choice, noul, TypeSafeClient } from "@typesafe-ai/sdk";
const client = new TypeSafeClient();
const triage = await client.systemOne({
model: "jev-latest",
state: {
task: "Verify that all customer emails in Postgres match standard RFC 5322 regex.",
affected_files: ["src/lib/validators.ts", "scripts/migrate-users.sql"],
},
questions: {
delegate_to: choice("Assign task to the most specialized subagent.", {
database_specialist: "SQL migrations, schema changes, and query optimization",
frontend_specialist: "UI components, styling, and client-side interactions",
backend_specialist: "Server routes, business logic, and API validation",
}),
requires_sandbox: noul("Does executing this task pose risk to live databases?"),
},
});
console.log(`Subagent: ${triage.answers.delegate_to.choice}`);
console.log(`Sandbox Required: ${triage.answers.requires_sandbox.noul > 0.85}`);
4. Loop Control: Continue, Retry, or Stop
A common failure mode in autonomous loops is agents getting stuck in repetitive loops or quitting prematurely.
Using Jev's Noul and Score primitives, you can evaluate loop termination criteria independently of the model generating the actions:
is_task_complete:Noulchecking if the user's objective is demonstrably met.is_loop_stuck:Noulchecking if recent actions are repeating without progress.solution_quality:Scoreevaluating the generated artifact before presenting it to the user.
control = client.system_one(
model="jev-latest",
state={"goal": user_goal, "history": agent_history[-4:]},
questions={
"is_complete": Noul(instructions="Does 'history' prove that 'goal' is fully achieved?"),
"is_stuck": Noul(instructions="Is the agent repeating identical commands or failing to make progress?")
}
)
if control.answers["is_complete"].noul > 0.90:
break
if control.answers["is_stuck"].noul > 0.80:
escalate_to_human_or_reset_strategy()
5. Pre-Execution Action Guardrails
Before an agent executes a potentially destructive bash command or database migration, Jev can evaluate safety guardrails within its fast service envelope:
guardrail = client.system_one(
model="jev-latest",
state={"proposed_command": "rm -rf /var/log/app/*", "environment": "production"},
questions={
"is_destructive": Noul(
instructions="Does 'proposed_command' permanently delete data or service files without recovery?"
)
}
)
if guardrail.answers["is_destructive"].noul > 0.75:
halt_execution_and_request_confirmation()
Related Context
System One Decision Models Architecture
conceptJev vs. Structured Outputs Comparison
comparisonJev Primitives: Choice vs. Score vs. Noul
comparison