Learn Hermes AI Agent - Adaptive Behavior & Reflection
Episode 18 of 23

Learn Hermes AI Agent - Adaptive Behavior & Reflection

This episode covers how an agent no longer runs rigidly: applying a reflective loop that evaluates its own results, measuring confidence and uncertainty before answering, and dynamically changing strategy based on feedback from both users and observability data.

AI Agent
AI AgentAugust 3, 2026
0 views
5 min read

Introduction

In episode 17 you built a multi-agent system: one orchestrator agent divides the work, delegates subtasks to other agents, and merges the results back together. But there is one question left hanging: what if the merged result is wrong? Previous episodes taught the agent to execute, but not yet to judge the results of its own execution.

Episode 18 answers that question. We will make an adaptive agent — one that can reflect on its output, is aware of its own confidence level, and changes strategy when the situation changes. This is a big leap: from an agent that merely "follows instructions" to an agent that "learns from results". Here is the roadmap for this episode:

  • Reflective loop: the act, observe, critique, revise cycle.
  • Self-correction before failures spread.
  • Monitoring confidence and uncertainty as signals.
  • Changing strategy based on feedback and data.

Reflective Loop: Act, Observe, Critique, Revise

The reflective loop is the heart of adaptive behavior. The idea is simple: do not let the agent answer once and be done. Give it the chance to judge its own answer, then improve if needed. The cycle follows four phases — act (do the work), observe (look at the result), critique (evaluate with criteria), and revise (fix the plan).

ts
import { HermesAgent } from "@hermes/sdk";
 
const agent = new HermesAgent({
  profile: "./profiles/support.ts",
  model: "gpt-4o",
});
 
async function reflectAndAct(task) {
  let attempt = 0;
  const maxAttempts = 3;
 
  while (attempt < maxAttempts) {
    const result = await agent.run(task);
 
    const critique = await agent.evaluate(result, {
      criteria: [
        "goal_met",
        "no_hallucination",
        "tool_used_appropriately",
      ],
    });
 
    if (critique.score >= 0.8) {
      return result;
    }
 
    task = critique.revisePlan(task);
    attempt += 1;
  }
 
  return agent.escalate(task);
}

Notice several important details in the code above. The maximum number of attempts is capped to prevent an infinite loop — there is always a clear upper bound. The evaluation criteria are explicit and domain-specific, not just "does the answer look good". And when all attempts fail, the agent chooses to escalate (hand off to a human) instead of giving a doubtful answer.

Self-Correction Before It Is Too Late

The reflective loop running within one conversation matters, but there is a more critical point: when the agent is about to call a tool. This is where the most expensive mistakes happen — a wrong SQL query, a deleted file, or an email sent to the wrong address. Self-correction at this point is more valuable than fixing the answer afterward.

ts
async function guardedToolCall(toolName, args) {
  const plan = await agent.draftPlan(toolName, args);
 
  const review = await agent.verifyPlan(plan, {
    rules: ["read_only_first", "no_destructive_on_production"],
  });
 
  if (!review.safe) {
    console.log(`Blocked: ${toolName} - ${review.reason}`);
    return agent.askHuman(toolName, args);
  }
 
  return agent.execute(toolName, review.sanitizedArgs);
}

The pattern above is called a guard layer: before the tool is executed, the agent drafts a plan, then verifies the plan against pre-defined rules. This is proactive self-correction — mistakes are prevented at the door, not fixed after they happen. Combine this pattern with the reflective loop from the previous section, and you have two layers of defense: one before the action, one after the result comes out.

Success

The combination of a guard layer before execution and a reflective loop after execution is the pattern we use for all production agents. The first layer prevents damage, the second layer continuously improves output quality.

Monitoring Confidence & Uncertainty

The reflective loop needs a signal to make decisions, and the most valuable signal is confidence — how sure the model is about its answer. A trained model is not always confident correctly: a confident-sounding answer is not necessarily accurate. That is why our agent needs to read its confidence level and act on thresholds.

ts
const reply = await agent.respond(input);
 
if (reply.confidence < 0.4) {
  await agent.askClarification(input);
} else if (reply.confidence < 0.7) {
  await agent.searchKnowledgeBase(reply.topic);
} else {
  await agent.respondDirectly(reply);
}

The thresholds you choose should be determined from data, not guesswork. The practical way: record the confidence score in every conversation, compare it with the final quality judged by a human, and then find the point where failures start increasing. That threshold is the minimum confidence that is safe for your domain.

Also note that uncertainty is not always bad. An agent that admits its ignorance and asks further questions usually produces higher user satisfaction than an agent that fabricates answers. Teaching the agent to say "I do not know, let me check" is a feature, not a weakness.

Changing Strategy Based on Feedback

Reflection and confidence give the agent self-awareness. But awareness without action is useless — the last piece is changing strategy when those signals indicate a problem. Strategy here means behavioral configuration: which model is used, whether answers use the cache, whether the allowed tools are narrowed, and so on.

ts
async function applyFeedback(feedback) {
  const stats = await agent.collectStats();
 
  if (stats.errorRate > 0.3) {
    await agent.applyStrategy({
      model: "gpt-4o-mini",
      allowRetries: false,
      escalateOnLowConfidence: true,
    });
  } else if (feedback.latencyMs > 2000) {
    await agent.applyStrategy({ enableCache: true });
  } else {
    await agent.applyStrategy({ allowCreativeModes: true });
  }
}

Strategy can be triggered by two feedback sources: explicit (a user presses the "not helpful" button, negative ratings) and implicit (error rate rising, latency ballooning, conversations breaking off mid-way). A good agent swallows both. To train it in a structured way, Hermes provides an evaluation hook you can call from the pipeline, for example hermes eval --suite regressions — old scenarios are re-run every time a strategy changes, so fixing one side does not break behavior that was already correct on another.

Avoiding Overfitting to Feedback

There is a trap to watch out for: adjusting too aggressively to short-term feedback. If one angry user causes the agent to immediately change its entire strategy, the agent will wobble following one person's opinion. Healthy adaptive behavior must be filtered through aggregation.

  • Collect feedback in a time window (for example, per day or per 1000 conversations), not per single event.
  • Distinguish feedback from verified users and anonymous users.
  • Apply strategy changes through an experiment group, not to all traffic directly.
  • Always have a rollback path: the old strategy must be restorable with one command.
apply-trial-strategy.sh
hermes strategy activate conservative --group 10% --ttl 24h

The command above applies the new strategy to only 10 percent of traffic for 24 hours. If the error rate does not drop within that window, just run hermes strategy rollback and everything returns to the previous strategy. This is the principle of safe change: prove it in a small group first, then generalize.

Conclusion

Episode 18 took you from an obedient agent to a reflective one: a reflective loop to evaluate and revise results, a guard layer to prevent mistakes before tool execution, monitoring confidence and uncertainty as decision signals, and a strategy-change mechanism filtered by aggregation and experiment groups.

Key takeaways:

  • A reflective loop needs explicit evaluation criteria and a clear attempt limit.
  • The most valuable self-correction happens before a tool executes, not after.
  • Confidence thresholds must be determined from data, not feelings.
  • Feedback must be aggregated in a time window and tested in a small group before wide rollout.
  • Adaptive behavior always keeps a rollback path.

In the next episode 19 we channel all this adaptive behavior into an orderly release process: CI/CD and a release pipeline specifically for agents — from testing behavior in CI and safely deploying model configurations, to versioning profiles and tools. See you there!

Learn Hermes AI Agent - Adaptive Behavior & Reflection | Learn Hermes AI Agent