<!-- Sub-agents and teams · Tin · https://tin.ad/docs/learn/teams/ · generated from the built page -->

[Learn](https://tin.ad/docs/learn/) · Agents · 20 of 34

# Sub-agents and teams

**Goal.** Build agents that call agents: pipelines, planners 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.tin · tin run sub-agent.tin*

```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'"))
}
```

<!-- output -->
```text
  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.tin · tin run pipeline.tin*

```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)
}
```

<!-- output -->
```text
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.tin · tin run critic.tin*

```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)
}
```

<!-- output -->
```text
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.

Your answer   Checking needs JavaScript; the solution below works without it.

**Solution

*task-1.solution.tin · tin run task-1.solution.tin*

```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)
}
```

<!-- output -->
```text
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.

Your answer   Checking needs JavaScript; the solution below works without it.

**Solution

*task-2.solution.tin · tin run task-2.solution.tin*

```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"))
}
```

<!-- output -->
```text
  Converter {"input":"ask the Converter: toF 30"}
mock: mock: 86
```

## Recap

- An agent can be another agent's tool.
- Pipelines pass one agent's typed reply to the next.
- A writer–critic loop is a few lines of ordinary code.

In the reference: [Sub-agents and concurrency](https://tin.ad/docs/ref/agents/#teams)
