Rlhf & Self-improvement

The RLHF pattern creates a self-improvement loop: generate output, have a critic agent score it, and store high-quality examples in a learning memory. Future generations reference these examples, so the system gets better over time.

Rlhf Self-improvement loop

The LLM weights do not change. Instead, high-quality examples are stored in memory and included as few-shot examples in future prompts. The system improves by giving the LLM better context, not by retraining it.

patterns/44_rlhf.py
python
class RLHFSystem:
    def process_task(self, input_text, task_type):
        output = self.generator.generate_with_learning(input_text, task_type)
        feedback = self.critic.critique_output(input_text, output, task_type)

        if feedback.score >= self.quality_threshold:
            self.learning_memory.add_example({
                "input": input_text,
                "output": output,
                "score": feedback.score,
                "category": task_type
            })
        return {"output": output, "score": feedback.score}

Generate-critique-learn loop with quality threshold.

Quiz: Quiz

Loading practice…

Flashcards: Flashcards

Loading practice…

The RLHF pattern turns every interaction into a learning opportunity, and the system literally gets smarter over time. Next up, we will explore two more exotic patterns: cellular automata for emergent swarm behavior and metacognition for agents that know their own limits.