Claude salvando contexto com /branch
Salvando contexto com /branch
Aprendi recentemente como usar branches no Claude Code corretamente para organizar o trabalho e manter o contexto sob controle.
O problema dos contextos grandes
O contexto cresce muito conforme a conversa avanรงa. Em algum momento, a IA tenta resumir tudo para economizar tokens e evitar o context rot.
Context rot is the performance degradation that happens when LLMs have to process increasingly long input contexts. Your LLMโs performance degrades when it has to search through longer contexts to find relevant information, even though that information is technically available in the context window.
Fonte: Context Rot โ Redis
Sugestรฃo
Existe o /branch no Claude, que basicamente separa sua conversa atual (incluindo o contexto) em uma nova thread. A nova thread tem o mesmo contexto de antes, e podemos fazer aรงรตes diferentes nela.
Entรฃo, vocรช estabelece o contexto adequado (a partir da sua conversa). E em vez de executar todas as sugestรตes de uma vez, cada novo branch cuida de uma separadamente (assumindo que nรฃo tรชm relaรงรฃo entre si).
Exemplo:
# Ruim
Faรงa um plano para melhorar meu site (context+)
[Claude gerou um plano com sugestรตes X, Y e Z]
Faรงa X (context++)
[Claude faz]
Faรงa Y (context+++)
[Claude faz]
Faรงa Z (context++++)
# Melhor
Faรงa um plano para melhorar meu site (context+)
[Claude gerou um plano com sugestรตes X, Y e Z]
Com isso, abra mais 2 abas e rode claude --resume em cada uma (com a primeira, temos 3 no total)
Aba 1
/rename X
Faรงa X (context++)
Aba 2
/branch Y
Faรงa Y (context++)
Aba 3
/branch Z
Faรงa Z (context++)
Exemplo (nรฃo) real
Vou mostrar um exemplo mรญnimo sรณ para explicar como funciona.
Contexto
Temos uma pรกgina simples de portfรณlio.
Adicionei 3 erros simples para serem corrigidos pela IA:
- Backend Engineer no topo e Fullstack Engineer no rodapรฉ
- Erros de digitaรงรฃo como โpassioanteโ
- Skill duplicada โHTML/CSSโ
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>John Doe โ Backend Engineer</title>
<style>
body { font-family: sans-serif; max-width: 700px; margin: 0 auto; padding: 20px; }
header, footer { background: #1a1a2e; color: white; padding: 16px; text-align: center; }
section { padding: 24px 0; border-bottom: 1px solid #eee; }
.skills span { background: #e0e7ff; padding: 4px 10px; border-radius: 12px; margin: 4px; display: inline-block; }
</style>
</head>
<body>
<header>
<h1>John Doe</h1>
<p>Backend Engineer</p>
</header>
<section id="hero">
<h2>Building robust systems from the ground up</h2>
<p>Available for freelance and full-time oportunities.</p>
</section>
<section id="description">
<p>I am a passioante developer with 5+ years of experianced building scalable APIs and microservices. I love clean code, performance tuning, and solving hard backend challanges.</p>
</section>
<section id="skills">
<h3>Skills</h3>
<div class="skills">
<span>Python</span><span>Node.js</span><span>PostgreSQL</span>
<span>Docker</span><span>HTML/CSS</span><span>HTML/CSS</span>
</div>
</section>
<footer>
<p>ยฉ 2026 John Doe โ Fullstack Engineer</p>
</footer>
</body>
</html>
Prompts
Plano:
โฏ Make suggestions to improve this portfolio.html. Up to 3, as this is just an example.
(...)
โ Updated plan
/plan to preview
User rejected Claude's plan:
โญโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฎ
โ Plan: Improve portfolio.html โ
โ โ
โ Context โ
โ โ
โ The user has a simple portfolio page (/tmp/claude/portfolio.html) for "John Doe โ Backend Engineer" and wants up to 3 improvement suggestions. The page has clear, fixable issues that hurt professional credibility. โ
โ โ
โ --- โ
โ Suggestions โ
โ โ
โ 1. Fix typos in the hero and description sections โ
โ โ
โ The page has four spelling errors that undermine credibility for a professional portfolio: โ
โ - Line 23: oportunities โ opportunities โ
โ - Line 26: passioante โ passionate โ
โ - Line 26: experianced โ experienced โ
โ - Line 26: challanges โ challenges โ
โ โ
โ File: portfolio.html, lines 23 and 26 โ
โ โ
โ --- โ
โ 2. Remove duplicate skill tag โ
โ โ
โ HTML/CSS appears twice in the skills list (line 33). One instance should be removed. โ
โ โ
โ File: portfolio.html, line 33 โ
โ โ
โ --- โ
โ 3. Fix inconsistent job title โ
โ โ
โ The <header> calls him a "Backend Engineer" but the <footer> says "Fullstack Engineer". These must match โ pick one and use it consistently (including in the <title> tag, which already says "Backend Engineer"). โ
โ โ
โ File: portfolio.html, lines 5, 17, and 39 โ
โ โ
โ --- โ
โ Verification โ
โ โ
โ After applying changes: โ
โ - Open portfolio.html in a browser and visually confirm the skills list, header, and footer โ
โ - Run a spell-check or re-read lines 23 and 26 to confirm no remaining typos โ
โฐโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโฏ
O contexto estรก em 22.3k/200k tokens (11%) (obtive isso rodando /context)
Agora temos 3 abas (esta primeira, e abrimos as outras 2. Vocรช tambรฉm pode usar tmux ou algo similar).
Na primeira, vocรช mantรฉm como estรก (ou usa /rename fix typos para facilitar a leitura). Na segunda, /branch remove duplicate skill tag. Na terceira, /branch fix inconsistent job title.
Agora temos 3 abas, cada uma com uma aรงรฃo especรญfica.
E entรฃo, em cada uma vocรช pede para resolver:
- Fix typos
- Remove duplicate skill tag
- Fix inconsistent job title
Com isso, vocรช pode ter tarefas do Claude (ou outra IA) rodando de forma simultรขnea e otimizada.
Contexto utilizado
Este รฉ sรณ um exemplo simples com prompts pequenos โ nรฃo รฉ um benchmark. Mas sรณ para ilustrar a ideia, fiz das duas formas e registrei o contexto.
Soluรงรฃo รบnica sem branch
Se fizermos tudo sem branches:
Prompt: Make suggestions to improve this website page. Up to 3, as this is just an example.
/context: 22.3k/200k tokens
Prompt: Fix typos
/context: 26.3k/200k tokens
Prompt: remove duplicate skill tag
/context: 28.6k/200k tokens
Prompt: fix inconsistent job title
/context: 31k/200k tokens
Dรก para ver que vai crescendo devagar (mesmo usando prompts bem pequenos).
Separando em branches
Quando dividimos em branches, cada um tem um contexto menor:
Aba 1:
Prompt: Make suggestions to improve this website page. Up to 3, as this is just an example.
/context: 22.3k/200k tokens
Prompt: Fix typos
/context: 26.3k/200k tokens
===========================
Aba 2:
Prompt: remove duplicate skill tag
/context: 26k/200k tokens
===========================
Aba 3:
Prompt: fix inconsistent job title
/context: 26.2k/200k tokens
Conflitos entre as tarefas
Este รฉ um exemplo com um รบnico arquivo. Mas mesmo em projetos maiores, os branches podem tocar os mesmos arquivos.
Vocรช pode evitar isso usando git worktrees (mas recomendo o worktrunk โ muito amigรกvel para devs, e cria uma pasta diferente para cada soluรงรฃo)
Worktrunk: worktrunk.dev
No meu caso, prefiro dar atenรงรฃo adequada a cada tarefa. Para coisas pequenas (como corrigir um typo) nรฃo preciso disso, mas para problemas maiores eu faria X, resolveria, commitaria. Depois (no outro /branch) faria Y, e entรฃo Z.