Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Retry Then Hand Off to a Person

In this video, you gave a bad answer one more try, with the error in the request, and sent every note the intake script can’t book to a person on the front desk’s list. One function decides who goes on that list.

Key Points

  • Nowhere to go. Before this lesson, the script held back a note whose answer failed the Zod schema and flagged a record the clinic can’t book, such as Noodle’s and Rolo’s. It printed them, and that’s all. Nobody at the clinic read them, and the one-word “help” note, which isn’t a booking, was printed with the others.
  • One retry for a bad answer. An answer can still fail the schema even though the provider gets one. As you saw in the last lesson, the request gives the model the pet name’s 40-character limit only as a hint in a description, and only Zod enforces it. The same request can bring back a different answer, so a second try can pass, and saying what was wrong gives the model something specific to fix.
  • Which answers get it. Only "invalid", an answer that fails the schema, and "not JSON" get a retry. The request goes in a constant, so the retry can send it again. For those two failures, the script prints a retry line, builds a request with the error, and asks once more with { ...request, ...retry }.
  • The retry replaces two keys. The keys of retry come later in the spread, so its instructions and input replace the first ones, while schema and role stay the same. The second result takes the place of the first, so result is now a let, and its cost adds to the total.
  • One retry at most. There’s no loop around the retry, so a note can’t get a second one.
  • The retry carries the failed answer. Each call to the model starts from nothing, so the retry has to carry the failed answer itself. In model/index.js, the "not JSON" and "invalid" failures now return the answer’s text too.
  • What the retry sends. withError(), in the new file retry-prompt.js, adds three lines to the instructions saying the last answer failed. The input keeps the note, and below it go the failed answer and the errors, each in its own tags. Each Zod issue becomes one line, the key and then the message.
  • The retry for one record. No answer failed in the runs for this video. Here’s the end of what withError() builds for the record you typed by hand in the last lesson, which no model sent. The errors name the pet’s name and the date:
Language: Program Output
<errors>
invalid
petName: Too big: expected string to have <=40 characters
preferredDate: Invalid ISO date
</errors>

Sent as a test, that retry came back valid all three times. In the first test, the model shortened the name to 'Pepper and Salt' and wrote the date as '2026-12-03'.

Locked learning resources

Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

Locked learning resources

The full lesson is for members only. Join us and get access to thousands of tutorials and a community of expert JavaScript developers.

Unlock This Lesson

Already a member? Sign-In

00:00 Your intake script holds back a note whose answer fails the Zod schema, and flags a record the clinic can’t book. Now you’ll give a bad answer one more try, and send every note it can’t book to a person.

00:14 Here’s the last run: Noodle and Rolo are flagged, and note eight, the one-word help, isn’t a booking. Nobody at the clinic reads them.

00:25 Take the record you typed by hand last lesson. The model gets the pet name’s limit only as a hint, so a real answer like that can arrive. A second try can bring a different answer, and the error gives the model something to fix.

00:40 Keep the request in a constant, so the retry can resend it. Make the result a let, because a retry replaces it. Only an answer that fails the schema, or isn’t JSON, gets a retry.

00:53 The script prints a retry line and builds a request with the error, which you’ll open next. Then it asks again, and the retry’s instructions and input replace the first ones.

01:04 Its cost adds to the total. There’s no loop around it, so one retry at most.

01:13 Each call starts from nothing, so the retry must carry the failed answer itself. In model/index.js, the "not JSON" and "invalid" failures now return the answer’s text too.

01:27 A new file, retry-prompt.js, builds the retry. Each Zod issue becomes a line under the failure’s name. Three new instruction lines say the last answer failed.

01:40 The input keeps the note and adds the failed answer and the errors, each in its own tags.

01:48 No answer failed in this video’s runs, so here’s the retry for your hand-typed record, which no model sent. Its errors are about the pet’s name and the date.

01:58 Sent as a test, it came back valid all three times, each costing about what note four’s first call did. A retried note costs about double.

02:09 The other failures skip the retry. A cut-off answer hit the output limit. The retry’s answer would be as long, so it’s likely to hit it again, as five notes did in an earlier lesson’s second low-limit run.

02:23 A refused answer, or one that stopped early for another reason, has no error to fix. The module’s sendWithRetries() tries a failed request up to four times.

02:35 Your new block retries a bad answer, once. The try goes around both calls, so a request that throws, first time or on the retry, is caught. The catch makes it a failure, request failed.

02:49 It gets no retry, so that note goes to a person too.

02:54 One function in a new file, front-desk.js, decides which notes go on the front desk’s list. It returns a reason, or null when the record is ready to book.

03:07 Any failure is a reason, and so is a record that isn’t a booking, or one checkRecord() flags.

03:14 In read-notes.js, delete the old held-back and flagged blocks, since frontDeskReason() does their work. Import it at the top, and withError() too.

03:24 Then start an empty list, frontDesk. A note with a reason goes on the list, and the loop moves on. After the loop, the script prints the list.

03:35 Under a failure, it logs the issues and the answer.

03:41 Run the set. Nine records are ready to book, and three notes are on the front desk’s list: Noodle, Rolo, and the one-word help.

03:52 There’s no retry line in this run. In 48 runs for this video, every answer passed the first time. Your run will differ. If an answer fails twice, the note still reaches a person, never a guess.

04:08 Retry a bad answer once, with the error. Send every other note the script can’t book to a person. Next, in the exercise Retry or Hand Off, you’ll write the step that reads one note, gives it its second chance, and says why the front desk should see it, if it should.

Become a Member to join the conversation.