Tin

Examples · agents · 32 of 339

03-team

Agents compose with everything else in the language.

examples/agents/03-team.tintin run examples/agents/03-team.tin
// Agents compose with everything else in the language:
//   - an agent in another agent's `tools` list becomes a tool (a sub-agent)
//   - `spawn` runs agents concurrently on the event loop
//   - `|>` chains them, because an agent is called like a function

/// Look up the population of a city, in millions.
tool fn population(city: string): float {
  sleep(150ms) // pretend this is a slow lookup
  match city {
    "Istanbul" => 15.7
    "Tokyo" => 14.0
    "London" => 8.9
    _ => 1.0
  }
}

/// Knows facts about cities.
agent Researcher {
  model: "mock"
  tools: [population]
}

/// Rewrites text for a general audience.
agent Editor { model: "mock" }

agent Lead {
  model: "mock"
  system: "Delegate research to the Researcher."
  tools: [Researcher]
  on toolCall(name, input) => print("  lead delegates to {name}")
}

fn main() {
  // a sub-agent call
  print(Lead("ask the Researcher for the population of 'Istanbul'"))

  // Three research tasks at once. One agent holds one conversation, so calls
  // to the same agent queue up; fresh() makes a copy with an empty history.
  let cities = ["Istanbul", "Tokyo", "London"]
  let t0 = now()
  let tasks = cities.map(c => spawn Researcher.fresh().ask("population of '{c}'"))
  for task in tasks { print(await task) }
  // one by one would take at least 450ms; in parallel it is about 150ms, so 300ms leaves room for a busy machine
  print(if now() - t0 < 300ms { "ran in parallel" } else { "ran one by one" })

  // a pipeline of agents
  let summary = "population of 'Tokyo'" |> Researcher |> Editor
  print(summary)
}
  lead delegates to Researcher
mock: mock: 15.7
mock: 15.7
mock: 14
mock: 8.9
ran in parallel
mock: mock: 14