Dayward AIWeChat Mini Program
From Frontend Engineer to Agent Engineer in 30 Days
Week 1 ยท D5๐Ÿ”’ LockedAbout 6 hours

The Tool System and Event-Driven Design: Parameter Validation, Feeding Errors Back for Self-Correction, Event Subscription (dg P05/P06/M05/M07)

Make the tool system solid: validate parameters, feed errors back to the model so it can correct itself, then use event subscription to expose the agent's internal state to an outside interface.

Today's goals 0/3

Sign in to tick these off and save your progress.

Today's Goals

  1. Add parameter validation to every tool, rejecting invalid input with a readable error
  2. Design a mechanism that lets the model see an error and correct its own call arguments
  3. Subscribe to the agent's event stream, outputting progress or typing status in real time

Yesterday closed on this: D4's fallback handles unreliable external services, and today's error feedback handles the model's own mistakes. A provider going down means switching vendors; a model filling an argument in wrong is nobody else's fault, and all you can do is let it try again. D2's tools were good enough to run; today they become tools that do not crash, that self-correct when they do, and that are visible from outside.

Plain-Language Walkthrough

A map of your tools: format constraints, cross-references, and the cost of a rename

Ordering from an online store, whether you buy the right item rests entirely on the title and the spec table: "multi-purpose storage box" leaves you guessing whether A4 paper fits, while "32 cm internal width" is something you can order from with confidence. A model picking a tool is in exactly that position โ€” D2 established that floor, and today we nail down the three ways of writing a definition that most often go wrong.

One, format fields with no valid example. The model has no concept of what an order number looks like, so it invents one by feel. Adding "for example SO20260901" is the highest-return line in the whole definition. Then write the same constraint into the schema as a pattern regex โ€” sent to the model it is a manual, and kept for the next section's validator it is a rule, and growing both from one place means they cannot drift apart.

Two, D2 said to write down when to use and when not to use a tool, without saying how โ€” the answer is cross-references between tools. The most valuable part of a description is often the negative space. Say only "look up an order" and the model will reach for it to look up refunds and shipping too; add "looks up the order itself only; use track_shipment for shipping events and query_refund for refund progress" and misuse drops by more than half immediately. The more tools you have, the less you are writing separate manuals and the more you are drawing a map of your tools.

Three, treating a tool name as a function name you may refactor on a whim. D2 said the name should read like a command; what it did not say is what the name becomes after launch โ€” a tool name is an external contract.

tools.js
// A tool definition is part of the prompt: this text enters the model's context verbatim
export const queryOrder = {
  name: 'query_order',
  description:
    'Look up one order status, amount and creation time by order number. ' +
    'Looks up the order itself only; use track_shipment for shipping events ' +
    'and query_refund for refund progress.',
  parameters: {
    type: 'object',
    properties: {
      order_id: {
        type: 'string',
        description: 'Order number, SO followed by 8 digits, for example SO20260901',
        pattern: '^SO\\d{8}$',
      },
    },
    required: ['order_id'],
  },
}

Writing it properly costs money, and you pay it again every turn: tool definitions are resent in full on every round. Using D2's figures, a solidly written tool runs about 100 to 150 tokens, so hanging 20 of them on the agent is a fixed overhead of two or three thousand per round, and a ten-round conversation burns twenty or thirty thousand on definitions alone. So only attach the tools the current scenario needs.

At this point the manual is solid. But no manual, however good, stops the next problem: the model generates arguments probabilistically, it is not a compiler. It will still write the order number as 12345 and the amount as a negative number. What does your tool do then?

Sign in and link WeChat to read

Sign in, then link your WeChat mini program account on the account page. Linked accounts share one free article per day. Unlock additional articles with rewarded videos in the mini program.

Mini program QR code

Scan in WeChat to open Dayward AI. Open the same course and chapter, choose the rewarded video, then refresh this page after completion.

Interview questions

  • What principles do you follow when designing tools for an agent โ€” how do you write the name, the description and the parameter schema?่ฎพ่ฎก Agent ็š„ๅทฅๅ…ทๆ—ถไฝ ไผš้ตๅพชๅ“ชไบ›ๅŽŸๅˆ™๏ผŸๅๅญ—ใ€ๆ่ฟฐใ€ๅ‚ๆ•ฐๅˆ†ๅˆซ่ฏฅๆ€Žไนˆๅ†™๏ผŸ
    Common in ChinaCommon overseasBasic#tool-design#prompt-engineering

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • When a tool call fails validation or errors out, how do you get the model to correct itself instead of failing the whole turn?ๅทฅๅ…ท่ฐƒ็”จๆŠฅ้”™ๆˆ–ๅ‚ๆ•ฐ้žๆณ•ๆ—ถ๏ผŒไฝ ๆ€Žไนˆ่ฎฉๆจกๅž‹่‡ชๅทฑ็บ ๆญฃ่€Œไธๆ˜ฏ็›ดๆŽฅๅคฑ่ดฅ๏ผŸ
    Common in ChinaCommon overseasIntermediate#tool-calling#error-handling

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • Which lifecycle events does an agent runtime typically expose, and why is waiting for the final return value not enough?Agent ็š„ไบ‹ไปถ็ณป็ปŸไธ€่ˆฌไผšๆšด้œฒๅ“ชไบ›็”Ÿๅ‘ฝๅ‘จๆœŸไบ‹ไปถ๏ผŸไธบไป€ไนˆไธ่ƒฝๅช็ญ‰ๆœ€็ปˆ่ฟ”ๅ›žๅ€ผ๏ผŸ
    Common in ChinaCommon overseasIntermediate#event-driven#observability

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

  • How do you bound an agent's tool permissions, and is putting the rules in the system prompt enough?ๆ€Žไนˆ้™ๅฎšๅทฅๅ…ท็š„ๆƒ้™่พน็•Œ๏ผŒ้ฟๅ… Agent ่ถŠๆƒๆ“ไฝœ๏ผŸๆŠŠ่ง„ๅˆ™ๅ†™่ฟ›็ณป็ปŸๆ็คบ่ฏๅคŸไธๅคŸ๏ผŸ
    Common in ChinaCommon overseasDeep dive#tool-permissions#security#prompt-injection

    ๐Ÿ”’ Unlock this chapter to see the answer keys.

Comments