Tin

Examples · agents · 34 of 339

05-claude

The same language features, with a real model. The model side replays a recorded tape, so it runs offline.

examples/agents/05-claude.tintin run examples/agents/05-claude.tin --replay tests/tapes/agents-05-claude.json
// The same language features, with a real model.
//
// Needs ANTHROPIC_API_KEY (or `ant auth login`) for the Anthropic API.
// Offline: tin run examples/agents/05-claude.tin --replay tests/tapes/agents-05-claude.json
// An agent without `model:` uses $TIN_MODEL, then claude-opus-5-5.
//
//   tin run examples/agents/05-claude.tin --trace

/// Add two integers and return the sum.
tool fn add(a: int, b: int): int => a + b

/// Multiply two integers and return the product.
tool fn multiply(a: int, b: int): int => a * b

agent Calculator {
  system: "You are a calculator. Use the tools for all arithmetic. Reply with just the number."
  tools: [add, multiply]
  on toolCall(name, input) => print("  call {name} {input}")
}

type Language = {
  name: string
  /// The year it first appeared
  year: int
  paradigm: Paradigm
  /// One sentence on what it is known for
  knownFor: string
}

type Paradigm = | Functional | ObjectOriented | Procedural | Logic | Multi

agent Historian {
  system: "You are a historian of programming languages. Be accurate and brief."
}

fn main() {
  // tools: the model decides which to call, the loop runs them
  let answer: int = Calculator("What is (17 + 25) * 3?")
  print("(17 + 25) * 3 = {answer}")

  // typed replies: ask for a list of structs, get a list of structs
  let languages: Language[] = Historian("Name three influential programming languages from before 1975.")
  for l in languages.sortBy((a, b) => a.year < b.year) {
    print("{l.year}  {l.name.padRight(8, " ")} {l.paradigm}  — {l.knownFor}")
  }

  // streaming: text handlers fire as the reply arrives
  let poet = agent {
    system: "You write very short poems."
    on text(chunk) => write(chunk)
  }
  poet("Two lines about the metal tin.")
  print()
  print("tokens used: {Calculator.tokens() + Historian.tokens() + poet.tokens()}")
}
  call add {"a":17,"b":25}
  call multiply {"a":42,"b":3}
(17 + 25) * 3 = 126
1957  Fortran  Procedural  — The first widely used high-level language, built for scientific and numerical computing.
1958  Lisp     Functional  — Brought in garbage collection, homoiconic code-as-data, and symbolic computation, and became central to AI research.
1972  Prolog   Logic  — The first major logic programming language, used in AI and natural language processing.
Bend it slow and hear it cry,
soft gray metal, Bronze's ally.
tokens used: 3332