Skip to main content
An AI agent works in a loop: it asks the model, runs the tools the model asks for, and asks again, until it has an answer. A single ticket can take a dozen model calls and a minute or more. If your app restarts in the middle, a plain loop starts over, pays for every model call again, and may repeat a tool call that already changed something. With Nelkap, each model call and each tool call is a step. When the run picks up after a crash or a deploy, the calls already made give back their saved results, and the agent goes on from the first call that hasn’t been made.
The steps of an answerTicket run: a model call that asks for an order, the order lookup, a second model call that writes the answer, the score, and the reply.The steps of an answerTicket run: a model call that asks for an order, the order lookup, a second model call that writes the answer, the score, and the reply.

Write the workflow

This agent answers a customer’s support ticket with Claude, using the Anthropic SDK. It has one tool, which looks up an order.
nelkap/workflows/answer-ticket.ts
On each turn, the workflow:
  1. Calls the model with the conversation so far, in a step named model.
  2. If the model has its answer, records how many turns it took, sends the reply, and ends.
  3. If the model asks for orders, looks up each one in a step named get order, and adds the results to the conversation for the next turn.
Replace getOrder and sendTicketReply with your own functions.

How the agent picks up after a restart

The conversation itself isn’t saved. On every call, the workflow rebuilds it from the saved steps:
  • Steps of the same name are matched in order. When the run picks up, the first model step gives back the first answer, the second gives back the second, and so on. The same goes for get order.
  • So the loop goes through the turns already made in an instant, without calling the model or your tools again. The model is called again only from the first turn with no saved answer.

Limits that keep the agent in check

  • The loop stops after 10 turns, so a confused agent can’t run forever. A run holds up to 100 steps.
  • If the model stops for any other reason, like reaching max_tokens, the run fails, and the reason shows on its page.

Start a run for each ticket

Register the workflow, and cap how many tickets are answered at once to stay within your model provider’s rate limits:
nelkap/index.ts
At most ten tickets are answered at once. The others wait their turn, in the order they arrived. Then start a run when a ticket arrives:

See it run

Open the run in your project’s runs. Each model call and each order lookup is a step, with exactly what it returned. When an answer looks wrong, you can read the turn where the agent went off track. The turns to answer score is charted over time, so you can see whether a change to the prompt or the tools makes the agent faster.

Ideas to extend it

  • Add more tools, each run in its own step.
  • Pause for a person’s approval before sending the reply, with step.wait, as in the refund approval example.
  • Record more scores, like whether the customer reopened the ticket.