Sn Tin

Learn Tin · Part 2: Agents · Chapter 20

Sub-agents and teams

Agents that call agents: planners, workers and critics.

One agent with twenty tools gets confused. A few small agents, each with a clear job, work better. In Tin, putting agents together uses the same things you already know: tools, function calls and |>.

An agent as a tool

List a top-level agent in another agent's tools, and it becomes a tool that takes one string, input. Its /// comment is the description the calling model sees.

sub-agent.tintin run sub-agent.tin
/// Look up the population of a city, in millions.
tool fn population(city: string): int => match city {
  "Istanbul" => 16
  "Tokyo" => 14
  _ => 1
}

/// Knows facts about cities. Ask it one question at a time.
agent Researcher { model: "mock", tools: [population] }

agent Lead {
  model: "mock"
  system: "Delegate fact-finding to the Researcher."
  tools: [Researcher]                     // an agent can be another agent's tool
  on toolCall(name, input) => print("  {name} {input}")
}

fn main() {
  print(Lead("ask the Researcher for the population of 'Istanbul'"))
}
  Researcher {"input":"Istanbul"}
mock: mock: 16

The mock: mock: is two agents talking: the Researcher's answer was mock: 16, and the Lead repeated it. Each agent keeps its own conversation.

Pipelines

An agent is called like a function, so |> feeds one agent's answer to the next:

pipeline.tintin run pipeline.tin
agent Writer { model: "mock", system: "Write a first draft." }
agent Editor { model: "mock", system: "Make the draft shorter and clearer." }

fn main() {
  // an agent is called like a function, so agents chain with |>
  let final = "a note about tin" |> Writer |> Editor
  print(final)
}
mock: mock: a note about tin

A writer and a critic

The most useful team is two agents and a loop: one writes, the other reviews, and your code decides when to stop. The critic's reply is typed, so the loop checks a real bool, not a sentence containing "looks good".

critic.tintin run critic.tin
type Review = {
  /// true when the draft is ready to publish
  ok: bool
  /// What to change, if anything
  advice: string
}

agent Writer { model: "mock", system: "Write and rewrite drafts." }
agent Critic { model: "mock", system: "Review drafts strictly." }

fn main() {
  var draft = Writer("a haiku about compilers")
  var round = 1
  loop {
    let r: Review = Critic("review this draft: {draft}")
    print("round {round}: ok={r.ok}")
    if r.ok || round == 3 { break }
    draft = Writer("rewrite it, staying true to this advice: {r.advice}")
    round += 1
  }
  print(draft)
}
round 1: ok=false
round 2: ok=true
mock: rewrite it, staying true to this advice: review this draft: mock: a haiku about compilers

The mock fills a bool with true only when the prompt contains "true", which is why round 2 passes. With a real model, the critic reads the draft. Either way, the round == 3 cap means a picky critic can't keep the loop going forever.

Code decides the control flow, and agents fill in the parts that need judgement. That keeps teams predictable: you can read the loop and know it ends.

Tasks

●○○ Task 1: Translate, then check

Write a pipeline of two agents: a Translator and a Checker that fixes its grammar.

Solution
task-1.solution.tintin run task-1.solution.tin
agent Translator { model: "mock", system: "Translate English to Turkish." }
agent Checker { model: "mock", system: "Fix any grammar mistakes in Turkish text." }

fn main() {
  let out = "good morning" |> Translator |> Checker
  print(out)
}
mock: mock: good morning

●●○ Task 2: A specialist

Give a Guide agent a Converter sub-agent, which has a tool toF that turns Celsius into Fahrenheit. Print the call the Guide makes.

Solution
task-2.solution.tintin run task-2.solution.tin
/// Convert Celsius to Fahrenheit, rounded.
tool fn toF(c: int): int => c * 9 / 5 + 32

/// Does unit conversions. Give it one conversion at a time.
agent Converter { model: "mock", tools: [toF] }

/// Answers travel questions.
agent Guide {
  model: "mock"
  tools: [Converter]
  on toolCall(name, input) => print("  {name} {input}")
}

fn main() {
  print(Guide("ask the Converter: toF 30"))
}
  Converter {"input":"ask the Converter: toF 30"}
mock: mock: 86

What's next

Next: spawn, await and within. Run agents at the same time, cancel them, and give them a time budget.

In the reference: Sub-agents and concurrency