08LLM DEEPSEEK_API_KEY

Agent

Give a model a tool and let it decide when to call it.

flowchart LR
  Q([question]) --> A[Agent]
  A -- tool call --> T[multiply]
  T -- result --> A
  A --> R([answer])

What it shows

  • The agent loops: ask the model, run requested tools, send results back.
  • maxIterations bounds the loop. output.toolCalls records every call.

The program

examples/agent/index.ts
import { Agent } from '@astack-tech/components';
import { ModelProvider } from '@astack-tech/integrations';
import { createTool } from '@astack-tech/tools';

const apiKey = process.env['DEEPSEEK_API_KEY'];
if (!apiKey) {
  throw new Error('Set DEEPSEEK_API_KEY to run this example.');
}

const multiply = createTool(
  'multiply',
  'Multiply two numbers.',
  args => Number(args['a']) * Number(args['b']),
  {
    type: 'object',
    properties: {
      a: { type: 'number', description: 'First factor' },
      b: { type: 'number', description: 'Second factor' },
    },
    required: ['a', 'b'],
  }
);

// The agent asks the model, runs the tools it requests, and repeats until the model answers.
const agent = new Agent({
  model: new ModelProvider.Deepseek({ apiKey }),
  tools: [multiply],
  systemPrompt: 'Use tools for arithmetic.',
  maxIterations: 5,
});

const output = await agent.run('What is 37 times 43?');

console.log(output.message);
console.log(output.toolCalls);
// [ { tool: 'multiply', args: { a: 37, b: 43 }, result: 1591 } ]

Run it

pnpm install && pnpm build

export DEEPSEEK_API_KEY=sk-...
pnpm --filter @astack-examples/agent start
CONCEPTAgentsLet a model call tools in a loop until it can answer.View the folder on GitHub