Tracking state inside workflow

I’m new to Temporal, so apologies if I’m getting concepts/terminology wrong.

I’m trying to figure out how to implement this LLM RAG workflow (simplified pseudocode):

// Allow up to 3 searches to generate an answer.
let remainingSearches = 3;
// Start with the user's search query.
let query = args.initialQuery;
let answer = '';
while (remainingSearches > 0) {

  // Perform search in an activity
  const searchResults = await search(query);
  // Call LLM to generate an answer in an activity.
  // LLM may decide to do a follow up search by passing back `nextQuery`.
  const answerResult = await generateAnswer({query, answer, searchResults});
  answer = answerResult.answer;
  if (answerResult.nextQuery) {
    query = answerResult.nextQuery;
  } else {
await saveAnswer(answer);

What’s the right way to model something like this?

In particular, I’m not sure where to keep workflow state like remainingSearches, answer, and query.

The workflow state is already durable. So, your code should work as it is.