Files
2nd/10_Wiki/Topics/Domain_Programming/AI_and_ML/Refinement.md
T
Antigravity Agent c24165b8bc refactor(topics): 멀티 에이전트용 지식 재편 — _Common(공통 기본기) + Domain_* 구조
에이전트 8종(대화형/프로그래머 C·S/디자이너/설계자/기획자/QA/PD/PM)에게
[공통 기본 능력 + 롤별 Specialty] 2층으로 지식을 주입하기 위한 재분류.
문서 내용·포맷은 무수정, 폴더 이동만 (6,372개 문서 수 보존 확인).

- Topic_Programming → Domain_Programming (내부 구조 보존)
- Topic_Graphic → Domain_Design
- Topic_Business → Domain_Product
- Topic_General → Domain_General
- _Common 신설: Math(구 Topic_Math_Specialty), Reasoning(구 General/From_Thinking & Reasoning),
  Reasoning_Creativity(구 General/From_창의성), Communication(Poetic_Blog_Writing + From_writing)
- 타 도메인의 From_* 폴더는 유지 (출처 표기일 뿐, 이미 도메인에 맞게 분류된 문서)
- 빈 폴더 정리 (memory/procedures)
- 에이전트→폴더 매핑은 workspace의 .astra/agent-knowledge-map.json (9개 에이전트)

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-07-11 11:05:56 +09:00

6.6 KiB
Raw Blame History

id, title, category, status, canonical_id, aliases, duplicate_of, source_trust_level, confidence_score, verification_status, tags, raw_sources, last_reinforced, github_commit, tech_stack
id title category status canonical_id aliases duplicate_of source_trust_level confidence_score verification_status tags raw_sources last_reinforced github_commit tech_stack
wiki-2026-0508-refinement Refinement 10_Wiki/Topics verified self
Iterative-Refinement
Self-Refine
Type-Refinement
none A 0.9 applied
llm
iteration
types
design
2026-05-10 pending
language framework
python anthropic-sdk

Refinement

매 한 줄

"매 첫 시도는 draft, 매 refinement는 product". Refinement는 초안을 critique → revise loop로 다듬어 quality를 끌어올리는 patterns의 family — LLM self-refine, type narrowing, design iteration 모두 매 같은 핵심 idea를 공유.

매 핵심

매 LLM Self-Refine (2024 Madaan et al. → 2026 mainstream)

  • Generate → Critique → Refine loop. Single model이 매 세 role을 모두 수행.
  • 매 효과: math, code, dialog 매 +520% accuracy without extra training.
  • Reflexion (Shinn 2023)은 verbal RL — 매 episode 끝에 매 self-critique를 episodic memory로 저장.
  • 2026 standard: Claude Opus 4.7 / GPT-5 매 native "extended thinking" mode가 매 refine을 internal로 흡수 → external loop는 매 high-stakes (legal, medical) 에서만.

매 Type Refinement (TypeScript / Flow / Python typing)

  • Narrowing: union type 의 매 instance를 specific subtype 으로 좁힘 (typeof, instanceof, discriminated union).
  • Refinement type: predicate-attached type — {x: number | x > 0}. Liquid Haskell, F* 매 사용.
  • 2026 TS 5.x: satisfies operator + control-flow analysis 매 강력 — 매 manual cast 의 거의 elimination.

매 Design / Spec Refinement

  • Stepwise refinement (Wirth 1971) — abstract spec → concrete implementation을 매 단계적으로.
  • BDD (Given-When-Then) 매 modern incarnation.
  • AI-aided: spec → Claude → multiple impl candidates → human picks → refine.

매 응용

  1. RAG answer 의 self-refine으로 hallucination ↓.
  2. Code generation 매 compile error → refine loop.
  3. TS API 의 progressive type narrowing.
  4. Product spec 의 PM ↔ AI iterative tightening.

💻 패턴

Self-Refine loop (Anthropic SDK)

from anthropic import Anthropic

client = Anthropic()
MODEL = "claude-opus-4-7"

def self_refine(task: str, max_iter: int = 3) -> str:
    answer = client.messages.create(
        model=MODEL, max_tokens=2048,
        messages=[{"role": "user", "content": task}],
    ).content[0].text

    for i in range(max_iter):
        critique = client.messages.create(
            model=MODEL, max_tokens=1024,
            system="You are a strict critic. List concrete flaws or reply 'NO_ISSUES'.",
            messages=[{"role": "user", "content": f"Task: {task}\n\nDraft:\n{answer}"}],
        ).content[0].text

        if "NO_ISSUES" in critique:
            return answer

        answer = client.messages.create(
            model=MODEL, max_tokens=2048,
            messages=[{"role": "user",
                       "content": f"Task: {task}\nDraft: {answer}\nCritique: {critique}\nRevise."}],
        ).content[0].text
    return answer

Reflexion-style episodic memory

class Reflexion:
    def __init__(self):
        self.memory: list[str] = []  # accumulated lessons

    def step(self, task: str) -> str:
        ctx = "\n".join(f"- {m}" for m in self.memory[-5:])
        attempt = llm(f"Task: {task}\nPast lessons:\n{ctx}\nAct.")
        feedback = environment(attempt)
        if not feedback.success:
            lesson = llm(f"Why did this fail? Task: {task}\nAttempt: {attempt}\nFeedback: {feedback}")
            self.memory.append(lesson)
        return attempt

TypeScript discriminated union narrowing

type Result<T> =
  | { kind: 'ok'; value: T }
  | { kind: 'err'; error: Error };

function unwrap<T>(r: Result<T>): T {
  if (r.kind === 'err') throw r.error;  // narrow → 'ok' branch
  return r.value;  // typed as T, no cast
}

Python TypeGuard refinement

from typing import TypeGuard

def is_str_list(x: list[object]) -> TypeGuard[list[str]]:
    return all(isinstance(i, str) for i in x)

def join(items: list[object]) -> str:
    if is_str_list(items):
        return ", ".join(items)  # narrowed
    raise TypeError("not str list")

Compile-error refine loop (code generation)

def codegen_refine(spec: str, max_iter=5):
    code = llm(f"Write Python for: {spec}")
    for _ in range(max_iter):
        ok, err = run_pytest(code)
        if ok: return code
        code = llm(f"Spec: {spec}\nCode: {code}\nFailing: {err}\nFix.")
    raise RuntimeError("refinement budget exhausted")

Best-of-N + judge (ensemble refinement)

def best_of_n(prompt: str, n: int = 5) -> str:
    candidates = [llm(prompt, temperature=1.0) for _ in range(n)]
    ranking = llm(f"Pick best of these:\n{candidates}\nReturn index 0..{n-1}")
    return candidates[int(ranking.strip())]

매 결정 기준

상황 Approach
Single-shot good enough (chat) No refine — extra cost
High-stakes (legal/medical) External self-refine + human review
Code with tests Compile/test-driven refine
Long agentic task Reflexion (episodic memory)
Reasoning math extended thinking (native) — already refine internally
TS API design Narrowing + satisfies

기본값: native extended thinking 우선 → 부족하면 external self-refine 12 iter.

🔗 Graph

🤖 LLM 활용

언제: high-stakes output, agentic loops, code with verifiable feedback. 언제 X: 매 latency-sensitive UX, 매 simple chat — extra latency × cost는 매 안 맞음.

안티패턴

  • Infinite refine loop: max_iter 의 hard cap 의 X → cost explosion.
  • Same-model critique only: 매 critic = generator인 경우 같은 blind spot. Mix models (Opus critic, Sonnet generator).
  • Refine without termination signal: "NO_ISSUES" 같은 매 explicit stop 의 부재 → 매 endless tweaking.
  • Type assertion 으로 narrow: TS 매 as 사용은 매 refinement 의 X — 매 unsafe cast.

🧪 검증 / 중복

  • Verified (Madaan 2024 Self-Refine, Shinn 2023 Reflexion, TS handbook narrowing).
  • 신뢰도 A.

🕓 Changelog

날짜 변경
2026-05-08 Phase 1
2026-05-10 Manual cleanup — full rewrite covering LLM self-refine, type narrowing, design iteration