AIによる翻訳。英語版は人による検証済みである。 原文を読む
教材キット
2026.11.19版(書籍の現行版)より。書籍が更新されると変わる。出典を明記すれば、自由に利用および改変できる。
シラバス
読書時間は章の本文に基づく。学習者はLabまたは自分のマシンでコード例も実行する。
週ごとに一章: 11 週
5 週間後
学習目標と概要
各章の学習目標はその冒頭の一文である。各構成案は章の節に従い、定義される用語、例を実行する前に求められる予測、各節が締めくくる規則を含む。 主要な用語と確認すべき予測は、クイズバンクが出題するとおり英語版のものである。
1. データエージェントをエンドツーエンドで作る
読了まで約54分。6 例。
この章を終えると、次のことができるようになる。
講義概要: 8 節
-
ショップとそのウェアハウスを知る
- 実行
load-the-warehouse.py - 実践ルール 実際の落とし穴を備えたウェアハウスに対してエージェントを構築すること。テスト行、キャンセル済み注文、複数の通貨、現地の営業日、会計カレンダー、そして紛らわしい名前を持つ旧式テーブルを少なくとも一つ含める。本番データを共有できない場合はシードから生成し、誰でも再構築できるようにする。整いすぎたデモ用スキーマは、まさに見るべき失敗を覆い隠してしまう。
- 実行
-
質問を投げて、モデルから SQL を得る
- 実行
first-question-to-sql.py - 実践ルール スキーマのみの text-to-SQL は、製品ではなく、上回るべきベースラインとして扱う。自分のウェアハウスで一度実行して返答を残しておくと、スキーマがどの定義を保持していないかが分かる。テストが依存するモデルの返答はすべて、モデルファイル、プロンプトのハッシュ、固定したサンプリングとともに記録し、見つけた失敗を他の人が再現できるようにする。
- 実行
-
Run the SQL and check the number
- 実行
run-the-model-sql.py - 実践ルール Never accept an agent's number because its SQL looks right. Check it against a golden result before anyone relies on it, and when the two differ, find the rows that explain the difference before you change anything. Expect the difference to be a definition, such as which orders count, more often than a syntax mistake.
- 実行
-
Answer from one defined metric
- 実行
one-metric-answer.py - 実践ルール Route a question to a defined metric before you let a model write SQL. Give the model the metric's description and its dimensions, and let it return a request, which your code validates and compiles. Write SQL from a model only for questions no metric covers, and only behind guards.
- 実行
-
Score both on five golden questions
- 実行
score-five-golden.py - 実践ルール Score every change on golden questions written from your own work, and report correct, wrong, error and declined separately. Include questions that should be declined from the start, or an agent that never declines will look perfect. Use a handful of questions as a smoke test while you build, and a larger set with a held-out split before you claim anything.
- 実行
-
1回の実行のトレースを読む
- 実行
print-one-trace.py - 実践ルール スコアだけでなくトレースを読み、拒否から先に読む。各拒否の理由が、「顧客数を数える指標がない」のように、エージェントに与えられたものについて述べているかを確認する。見ていないデータについての主張になっていないかを確かめる。理由が誤っている拒否は、スコア上は拒否として数えられていても、欠陥として扱う。
- 実行
-
What goes wrong in production
- 実践ルール Before anyone outside your team uses the agent, make every failure above visible: score on golden questions in CI, show provenance and data freshness with each answer, review declines in traces, replay recorded replies by message hash, and run model-written SQL only on a read-only connection. Start with golden scoring, because it is the only one of these checks that catches a plausible wrong number.
- この章で下した決定
2. 生の text-to-SQL が数値を誤る箇所
読了まで約59分。7 例。
本章を読み終えると、次のことができるようになる。
講義概要: 8 節
-
Joins that fan out and inflate a sum
- 実行
tent-orders-fan-out.py - 実行
figure-lines-per-order.py - 実践ルール When a query joins tables of different grains, check the grain of every column it sums. Aggregate the finer table to the coarser grain before joining, or filter with
INorEXISTS, and compare the row count after the join with the count of distinct keys. Expect a model given only the schema to miss this, because the schema names columns, not grains.
- 実行
-
スキーマが保持しない定義
- 実行
gross-or-net-revenue.py - 実践ルール 問いに含まれるビジネス用語はすべて、照合すべき列ではなく、調べるべき定義として扱うこと。各定義を誰が所有しているかを書き留め、エージェントには、計算の元になり得る列だけでなく、定義そのものを、管理されたメトリクスとして、少なくとも用語集の項目として与えること。ある語に文書化された定義がない場合は、エージェントに、選ばせるのではなく、確認させるか回答を控えさせること。
- 実行
-
Time zones and the fiscal calendar
- 実行
fiscal-year-and-local-day.py - 実践ルール Give the agent your calendar, not only your columns: which timestamp is UTC, which date is the business day, and when the fiscal year starts and how it is named. Test every period question at a boundary, such as the first day of a month or the first month of a fiscal year, where UTC and local days or calendar and fiscal years disagree. Prefer a date dimension the agent joins over date arithmetic it writes.
- 実行
-
The table with the tempting name
- 実行
legacy-table-answer.py - 実践ルール List the tables an agent may read, and leave legacy and staging tables off the list even when they have the friendliest names. Check every model-written query's tables against the list before it runs. When an agent never chooses a tempting table in your tests, keep the check anyway: a new question, a larger schema or a retrieved example can change that.
- 実行
-
Filters nobody wrote down
- 実行
cancelled-lines-counted.py - 実践ルール Move every rule about which rows count into the marts where you can, so no query can forget it. Where a mart has to keep the rows, as with cancelled orders, write the rule into the definition the agent uses, and add a golden question that fails when the rule is left out. Read the gold for the same rules, because the person who wrote it can miss them as readily as the model can.
- 実行
-
Count the failures by kind
- 実行
classify-schema-only-errors.py - 実践ルール Count failures by kind on your own golden questions before you choose a fix, and keep errors that fail loudly apart from wrong numbers that don't. Label kinds by comparing each query with its golden SQL, with rules you can read, and read the wrong answers no rule labels. Fix the most common kind of wrong number first, and expect it to be a definition, a calendar or a filter more often than a syntax mistake.
- 実行
-
本番で何がうまくいかなくなるか
- 実践ルール エージェントの数値が誰かに届く前に、すべての回答を正解結果と照らして採点し、期間の境界とすべての定義に対する正解質問を維持し、エージェントが読めるテーブルを列挙し、誤答の標本を手で読む。誤った数値は種類別に数え、どの種類であれ件数が増加していれば警報として扱う。
- この章で下した判断
3. 自分の業務から golden question を作る
読了まで約54分。9 例。
この章を終えると、自社の業務で人々が尋ねる質問から golden question を書けるようになる。
講義概要: 9 節
-
公開ベンチマークは自分のテストにならない理由
- 実行
public-vs-own-score.py - 実践ルール 公開ベンチマークは、共通のタスクでモデルを比較するために使い、エージェントが自分のウェアハウスで動作する証拠としては使わない。gold セットは修正されるので、引用するスコアには必ずベンチマークのバージョンを添える。エージェントは、自分の業務から自分の定義で書いたゴールデンクエスチョンで測定し、二つのスコアが食い違うことを前提にする。
- 実行
-
Write questions from the work people do
- 実行
load-golden-questions.py - 実践ルール Collect golden questions from real requests, keep the asker's wording, and give each one a role, a kind and a note that names the definition its gold follows. Write the golden SQL against the marts the agent will use, and have the person who owns the definition read the note. Expect to spend more time on the definitions than on the SQL.
- 実行
-
Compare results, not SQL text
- 実行
results-match-rules.py - 実践ルール Score an agent by comparing its result with the golden result, never by comparing SQL text, normalized or not. Keep one comparison function for every score you report, so a change in the score is a change in the agent. When a match surprises you, read the agent's SQL, because a result can match by coincidence.
- 実行
-
順序、丸め、列名
- 実行
tolerant-comparison.py - 実践ルール 行の順序、列の順序、列名は無視し、余分な列は許し、行数が同じであることと、数値が小さな相対許容幅内にあることを求める。各正解値は明示した桁に丸め、その桁の半分の単位を上乗せして許容する。
order_mattersは、質問が順位または順序を求めている場合にのみ設定する。単位と桁の尺度は質問またはその注記で固定し、他の単位による回答は誤りとして扱う。それを読む人には単位の違いが分からないからである。
- 実行
-
Check the gold before you trust it
- 実行
gold-runs-and-agrees.py - 実行
gold-fan-out-audit.py - 実行
reworded-golden-question.py - 実践ルール Before a golden question scores anything, run its SQL, reject empty and NULL results, compare it with an independent query where you can, and have the definition's owner read its note. Read the agent's wrong answers for unclear questions, and fix the question, not the score. When a question can be read two ways, name the reading in its text, or make it a decline.
- 実行
-
回答だけでなく、拒否とロールも網羅する
- 実行
golden-coverage.py - 実践ルール 拒否と禁止された質問は最初から含め、拒否は曖昧と回答不能に分ける。正しい答えが異なるからである。各質問に、それが試験するトラップのタグを付け、採点の前に、分割ごとに種類、ロール、トラップを数える。不足は新しい質問を書いて埋める。エージェントがその質問にどう答えるかを見たあとで、既存の質問を分割間で移してはならない。
- 実行
-
テスト分割を取り分けておく
- 実行
baseline-on-both-splits.py - 実践ルール ゴールデン質問は、何かを調整する前に一度だけ分割し、調整は dev だけで行う。test 分割を採点するのは結果を報告するときだけとし、報告では両方の分割を並べて示す。test の質問が何度も見られて、人がそれに合わせ込むようになったら、新しい test 質問を書き、古いものは dev に移す。
- 実行
-
本番環境で起こる問題
- 実践ルール ゴールデンセットはコードとして扱う。定義の隣に置き、そのチェックを CI で実行し、ゴールデン結果は採点時に計算し、実際のトラフィックから得た質問を dev に追加し、テスト分割を守る。スコアが動いたときは、セットや比較を変更する前に、変化した回答を読む。
- この章で下した決定
4. エージェントに階層化したコンテキストを与える
読了まで約67分。10 例。
この章を終えると、SQL を書くモデルにスキーマ以上の情報を、層にして与えられるようになる。
講義概要: 9 節
-
What the schema says, and what it doesn't
- 実行
schema-digest.py - 実践ルール Keep the whole schema in the context when it fits, and add meaning to it rather than taking tables away. Before you write any other layer, take one real question and list every reading of the schema that answers it. Each reading the schema doesn't rule out is a rule some other layer has to state.
- 実行
-
Describe columns where names mislead
- 実行
column-descriptions.py - 実践ルール Write notes only for tables and columns whose names mislead: deprecated tables, amounts with a sign or unit you wouldn't guess, timestamps in a time zone other than the business's, and columns whose values include a placeholder such as
(none). Write them as facts about the column, not as instructions for one question. Expect a note to fix many questions and occasionally mislead on one, and keep the questions it broke in the golden set so you see it happen.
- 実行
-
ビジネス用語集を追加する
- 実行
glossary-context.py - 実践ルール 意味が誰かの選択によって決まるビジネス上の語には、それぞれ用語集のエントリを書き、テーブルと列を用いて記述する。各規則は、列の注記か用語集のエントリのどちらか 1 か所に置くことが望ましい。ショップのファイルはキャンセルされた注文の規則を両方に書いているが、
gq-002での見落としは、2 度書いても定着しないことを示している。用語集は信頼する前に dev の質問で測定し、そこで効果があるか、次の層がそれに依存する場合にのみ残す。
- 実行
-
検証済みクエリを例として取得する
- 実行
retrieve-similar-queries.py - 実践ルール 質問ごとに少数の検証済みクエリを、マスクした質問の類似度で選んで取得し、それぞれを質問と SQL として示す。最小スコアを設定し、ウェアハウスが答えられない質問でそれを確認すること。語彙に基づく取得は必ず何かを返すからである。正解付きの質問を検証済みの集合に入れてはならない。埋め込みへ移るのは、人が似ていると見なすクエリを語彙に基づく取得が逃していることを、自分たちの測定が示したときに限る。
- 実行
-
コンテキストを予算に収める
- 実行
context-token-budget.py - 実践ルール トークン予算はモデルのウィンドウよりも十分に内側に設定し、各質問のコンテキストは、層を書いた順ではなく、測定した優先順位によって予算に収める。スキーマが収まるなら、決して捨てない。何を捨てたかにかかわらず、安定した層を先頭に、質問を最後に組み立てる。トークンの推定値はモデルが実際に数えた値と一度照合し、高めの側に保つ。
- 実行
-
Measure what each layer adds
- 実行
layer-ablation-dev.py - 実行
figure-layers-correct-and-tokens.py - 実行
missing-columns-schema-only.py - 実行
what-stays-wrong.py - 実践ルール Measure context one layer at a time on the dev split, with the same questions, comparison rule and model, and keep the outcomes per question, not only the totals. Treat a difference of one or two questions on a set this size as noise until repeated runs say otherwise. Read the queries that fail, because each one names a layer you don't have yet or a rule the model still skips.
- 実行
-
選んだ層を test で確認する
- 実行
best-layers-on-test.py - 実践ルール 層の組は dev で選び、test 分割でベースラインと並べて一度だけ採点し、報告するのは test の数値とする。dev の結果を読んだあとにコンテキストへ加えた変更はすべて記録しておく。そのそれぞれが dev のスコアを過大にしうるからである。dev と test が食い違うときは、原因を説明する前に、各分割が手元の例にどれだけ近いかなど、測定できることを確認する。
- 実行
-
What goes wrong in production
- 実践ルール Version the context with the warehouse: keep it beside the marts, checked in CI against the tables it names, scored on golden questions with every change, and logged per answer with the verified queries it used. Move any rule that must always hold out of the context and into a definition the model can't skip.
- Decisions this chapter made
5. セマンティックレイヤーで指標を一度だけ定義する
読了まで約59分。9 例。
この章を終えると、次のことができるようになる。
講義概要: 9 節
-
各テーブルをセマンティックモデルとして記述する
- 実行
load-semantic-models.py - 実践ルール テーブルごとに1つのセマンティックモデルを宣言し、一意である場合にのみエンティティをプライマリとし、ウェアハウスまたは YAML が変わるたびに、宣言をデータに対して検査すること。シンプルメトリクスは、モデルと集計を指定してメジャーなしで書く。ただし、使用している MetricFlow のバージョンや dbt プロジェクトがまだメジャーを必要とする場合は除く。
- 実行
-
Compile a metric request to SQL
- 実行
compile-net-revenue.py - 実践ルール Let the agent, and every other client, send metric requests, never SQL fragments. Resolve every name in a request against the definitions, build the query as a tree with values as literals, and show the SQL with every answer so a person can read what ran. Accept a raw SQL filter only from the people who maintain the layer, never from a model.
- 実行
-
Join through entities so sums can't fan out
- 実行
entity-join-no-fanout.py - 実行
figure-entity-graph.py - 実践ルール Declare entities so that every join the layer makes is many rows to one, and let the layer refuse the rest. When a question needs a number on a finer table, such as revenue by product, define the metric on that table rather than reaching it through a join. Check that primary entities are unique in the data, because a wrong declaration turns the protection off.
- 実行
-
単純な指標から比率指標と派生指標を作る
- 実行
refund-rate-metric.py - 実践ルール あらゆる率を2つの指標の比率として定義し、各辺を個別に集計してからグループごとに割る。各辺がどの時間ディメンションで数えられるかを書き留め、各辺が、自身の定義の中であれ入力フィルタとしてであれ、フィルタを適用していることを確認する。読者がそれを知っているものと期待してはならない。式が1回の除算にとどまらない場合は派生指標を使い、その入力は単独でクエリできる単純な指標にしておく。
- 実行
-
会計カレンダーをタイムスパインに置く
- 実行
fiscal-quarter-grain.py - 実践ルール 会計カレンダーや業務カレンダーはすべて、値がそれぞれ1つの期間を指し示すカスタム粒度としてタイムスパインに置き、リクエストにはそれらでグループ化させる。現地の営業日は、レイヤーより前のマートで作成する。会計期間をまたぐ比較が必要な場合は、使用するレイヤーがカスタム粒度でのオフセットに対応するまで、期間ごとに1回ずつリクエストを実行し、結果をコード上で比較する。
- 実行
-
定義を MetricFlow で検証する
- 実行
metricflow-agrees.py - 実行
metricflow-needs-a-date.py - 実践ルール 自分のレイヤーを MetricFlow、または本番でそれを提供するツールと、依拠するすべてのリクエスト形式で比較し、どちらかが変わるたびにその比較を実行する。MetricFlow のバージョンは厳密に固定し、そのエンジンは一つのモジュールの背後に置く。Python API はどのリリースでも変わりうるからである。
- 実行
-
一つの定義、任意のウェアハウスの方言
- 実行
transpile-metric-sql.py - 実践ルール 定義は一つに保ってトランスパイルする。ただし、各方言の SQL は、そのウェアハウス上で既知の答えと照らして実行するまでは、未検証として扱う。コンパイラ側で日付と除算を明示的にキャストし、翻訳が推測する部分を減らす。
- 実行
-
本番環境で起こる問題
- 実践ルール セマンティックレイヤーはコードと同じようにテストする。エンティティの一意性をデータに対して確認し、すべてのメトリクスのSQLをCIで実行し、変更のたびにMetricFlowおよびゴールデンクエスチョンと比較する。レイヤーを保守する人以外からは、構造化されたリクエストだけを受け付ける。
- この章で下した決定
6. まず管理されたメトリクスに質問をルーティングする
読了まで約69分。7 例。
この章を終えると、次のことができるようになる。
講義概要: 8 節
-
Give the agent metrics, not tables
- 実行
metric-tools.py - 実践ルール Give the agent the metrics, their descriptions and the dimensions each can use, and keep tables out of its tools until a question needs them. Put the catalog in the prompt while it fits, so a run doesn't spend a call listing metrics, and let a describe tool return dimension values from the warehouse. When the catalog outgrows the prompt, keep names and descriptions there and move the dimensions behind the describe tool.
- 実行
-
Ask for a request, not SQL
- 実行
model-returns-a-request.py - 実践ルール Have the model return a metric request as structured arguments to a tool, and parse it into a typed request before anything else happens. Keep the reply format simple enough for your own code to parse, and keep that parser in one place. Accept SQL from a model only on a separate, guarded path, for questions no metric covers.
- 実行
-
Validate the request against the layer
- 実行
reject-unknown-dimension.py - 実践ルール Validate every request before it compiles: names against the layer, categorical values against the warehouse, dates against their grain. Refuse with a message that names the allowed alternatives, and send it back to the model, which can usually correct itself in one call. Treat any request that would compile into an empty or partial result as invalid, not as an answer of zero.
- 実行
-
Ask back when a question is ambiguous
- 実行
clarify-best-customers.py - 実践ルール Give the agent a tool to ask back, with options that name the layer's metrics, and show the person each option's definition. Ask back only when every option leads to a request the layer can answer; when none does, decline instead. Read clarifications in traces as you read declines, because a score counts a useless question as correct.
- 実行
-
ゴールデン質問でメトリクス優先方式を測定する
- 実行
metrics-first-score.py - 実行
figure-outcomes-by-approach.py - 実践ルール メトリクス優先方式は、同じゴールデン質問でベースラインと並べて採点し、調整は dev だけで行う。調整中に dev 分割を何回記録したかを明記し、テスト分割はその後に一度だけ記録する。正解、誤答、エラー、辞退、ステップ上限で終了した実行は別々に報告し、確認と拒否されたリクエストも数える。これらは呼び出しがどこに使われるかを示すためである。
- 実行
-
Read the runs that weren't correct
- 実行
metrics-first-misses.py - 実践ルール Read every run that wasn't correct, call by call, and sort it by what it needs: a near-miss metric means the descriptions need sharpening or the question needs asking back, a missing metric means the layer needs one or the question needs guarded SQL, and an answer that depends on the role means access rules, not prompts.
- 実行
-
What goes wrong in production
- 実践ルール Before you widen the agent's audience, keep one metric per business term with descriptions that say when not to use it, review validation messages as you review prompts, read dimension values when requests run, enforce roles below the agent, and count clarifications and refusals beside the score. Start with the near-duplicate metrics, because they produce valid requests that return wrong numbers.
- この章で下した決定
7. エージェントにガード内でSQLを書かせる
読了まで約87分。12 例。
この章を終えると、どの指標にも当てはまらない質問を、エージェントにSQLを書かせて処理できるようになる。
講義概要: 12 節
-
Give the agent a database with only what it may read
- 実行
agent-warehouse.py - 実践ルール Run the agent's SQL on a connection that can reach only what the agent may query: a role with only those grants, or, in DuckDB, a read-only database file that holds only the allowed tables, with file access and extension loading off and its settings locked. Name the allowed tables one by one. Then list what the database can't hide, such as its catalog, and refuse those reads in the guard.
- 実行
-
各ロールにビューのデータベースを与える
- 実行
role-database.py - 実践ルール データベースを境界にする。エージェントは、そのロールが見てよい行と列を制限するビューだけを読めるロールとして、読み取り専用で接続する。ウェアハウスにセキュアビュー、行アクセスポリシー、マスキングポリシーがあるなら、それらを使う。DuckDB では、ロールごとにそれらのビューを収めた読み取り専用のデータベースを与える。ビューはスタックの他の部分が使うのと同じポリシーから生成し、パーサーが受け入れるクエリでテストする。
- 実行
-
実行前にSQLを構文解析する
- 実行
parse-agent-sql.py - 実践ルール エージェントのSQLは、何かを実行する前に、実行するウェアハウスの方言で木に構文解析し、ちょうど1つの文だけを受け付ける。検査するのはノードであり、テキストではない。構文解析できたクエリは、有効なクエリではなく、形を検査できるようになったクエリとして扱う。ウェアハウスに送る前に、そのクエリが読み取ってよいテーブルに照らして列を解決する。
- 実行
-
Allow only read-only queries on known tables
- 実行
block-a-delete.py - 実行
block-raw-tables.py - 実践ルール Accept only a query at the root, walk every node for a statement that writes, and allowlist tables by schema and name, with nothing but tables and subqueries in
FROM. Resolve a name without a schema as a CTE only where a CTE of that name is visible, refuse the database's catalog by name, and allow only the functions your analytics need. Check the same list the agent's database holds. When a golden question needs a table off the list, give it a mart or a metric, not a wider list.
- 実行
-
Bound the rows and the cost
- 実行
explain-before-running.py - 実行
figure-plan-row-estimates.py - 実践ルール Bound every query four ways: a row limit that keeps a smaller one the agent wrote, a limit on the result's size, a plan-size limit from
EXPLAINchecked before the query runs, and one deadline over every step of the guard, planning included. Refuse a result over either limit rather than cut it, and refuse a plan with a step that has no estimate. Set the plan limit from your own known-good queries. Never useEXPLAIN ANALYZE, which runs the query, and rely on the deadline and the result limits, not the estimate.
- 実行
-
Enforce the deadline by killing a process
- 実行
stop-a-slow-plan.py - 実践ルール Start the deadline when the agent's SQL arrives, and run
EXPLAINunder it as well as the query. Enforce it by killing a separate process with its own read-only connection, not with an interrupt. Check on your own database that a stopped plan and a stopped query really stop.
- 実行
-
Replay the attacks on the guard
- 実行
replay-guard-bypasses.py - 実践ルール Rely on the database's limits, and treat the parser as defence in depth. Keep every attack anyone finds as a test: replay it against the agent's database alone and through the guard, and check that each is refused by the layer you expect. When an attack gets past the parser, ask first why the database let it reach anything, and then fix the parser.
- 実行
-
Repair once from the error
- 実行
repair-from-error.py - 実践ルール Repair at most once, and only from an outside signal: the guard's refusal or the warehouse's error, passed to the model with its own SQL and a prompt that doesn't assume the SQL is wrong. Let the model decline in the repair. Score repaired answers on their own, apart from first-time answers, and if your golden questions show they are rarely right, decline on a refusal instead of repairing.
- 実行
-
Transpile to the warehouse's dialect
- 実行
transpile-to-snowflake.py - 実践ルール Let the agent write the dialect your golden questions are checked in, guard that SQL, and transpile the guard's output, not the agent's text, with errors raised for what the transpiler knows it can't translate. Drop the guard's quotes for warehouses that fold unquoted names to upper case, refuse functions the transpiler passed through, and run the golden questions on the target warehouse before you trust a transpiled answer, because a function with the same name can return different values.
- 実行
-
When to prefer a new metric over SQL
- 実行
long-tail-score.py - 実践ルール Send a question to guarded SQL only when no metric covers it, and score that path on its own long tail of golden questions. Promote a question to the semantic layer when it recurs, when its answer goes into a report, or when guarded SQL gets it wrong on a definition, as with what counts as an order. Leave one-off lookups on the guarded path. When a question needs a table off the allowlist, build the mart and its metric rather than widening the list.
- 実行
-
本番で起こる問題
- 実践ルール ロールの読み取り専用のビューのデータベースを統制の本体とし、ガードは誤りを拒否に変えてコストを抑える層として扱う。エージェントの権限を確認し、各ロールのビューをテストし、すべての正解クエリを CI でガードに通し、時間制限をガードとウェアハウスの両方に設定し、拒否、見積もり、修復のすべてを答えとともに記録し、変更のたびに、その経路を、独自のロングテールの正解付き質問で採点する。
- この章で下した決定
8. 答えられないときは、そう伝える
読了まで約57分。6 例。
この章を読み終えると、読者は辞退を正式な応答の一つとして扱えるようになる。
講義概要: 7 節
-
ウェアハウスが答えられない質問の種類
- 実行
unanswerable-cases.py - 実践ルール ゴールデンセットで辞退とされた質問はすべて、曖昧か回答不能かに分類し、カテゴリを明記して、どの返答が必要かを把握する。曖昧な質問に問い返すのは、すべての解釈が回答可能な場合に限る。回答不能な質問は、何が欠けているかを示して辞退する。回答不能な質問ごとに、モデルが答えと取り違えかねない近くの列や値を探す。それが、防ぐべき誤った数値だからである。
- 実行
-
Decline with a reason
- 実行
cannot-answer-tool.py - 実践ルール Make declining a tool that ends the run with a kind from a fixed list and a sentence for the person. Let the model choose among the kinds it can judge, such as missing data, a forecast or an undefined term, and let code decide the kinds code can check, such as stale data or a forbidden role. Count declines by kind, and read the reasons in traces, because a decline of the right kind can still give the wrong reason.
- 実行
-
回答の前に鮮度を確認する
- 実行
stale-refunds.py - 実行
figure-refund-settling.py - 実践ルール 期間を挙げるすべての回答の前に、コードで鮮度を確認する。回答が読む各フィードの最新の読み込みを、期間の終わりに指標の整定期間を加えた時点と比較し、フィードがそこに届いていなければ、行が読み込まれていない場合も、まだ発生していない場合も、stale として辞退する。各整定期間は自分のデータから、見てきたイベントを覆う遅延として測る。読み込み時刻は、自分の読み込み列か読み込みログから読み、テーブルメタデータからは決して読まない。各テーブルを、その鮮度を決めるフィードに対応づける。期間のない問いには答え、来歴にそのデータの時点を示す。
- 実行
-
Show the provenance with every answer
- 実行
answer-with-provenance.py - 実践ルール Show every answer with its provenance, and put the metric's name and definition first, since that's where a near-miss metric shows. Then show the filters, the period, the role and the data as of, and keep the SQL one step away for the people who check it. Show declines the same way, with their kind, reason and role.
- 実行
-
Measure declines, not only accuracy
- 実行
decline-precision-recall.py - 実践ルール Score an agent on declines as well as answers: report decline precision and recall, coverage and selective risk, and a penalty score with a c your stakeholders agree to, stated as a judgement. Compare agents on the same questions, and list the wrong numbers each gave, because a change that removes one wrong number can add another. When the decline questions number in single digits, read the runs, not only the rates.
- 実行
-
本番環境で何がうまくいかなくなるか
- 実践ルール より多くの人がエージェントを使うようになるまでは、鮮度チェックにロードログを与え、各指標の落ち着き期間を定期的に測り直し、すべての時系列の最後の期間をチェックし、拒否を種別とフィードごとに数え、理由と聞き返しを一部抽出して読む。まず、答えられる質問に対する
not_in_dataの拒否から始める。それぞれが、書くべき指標を一つずつ示しているからである。
- 実践ルール より多くの人がエージェントを使うようになるまでは、鮮度チェックにロードログを与え、各指標の落ち着き期間を定期的に測り直し、すべての時系列の最後の期間をチェックし、拒否を種別とフィードごとに数え、理由と聞き返しを一部抽出して読む。まず、答えられる質問に対する
- この章で下した決定
9. エージェントに閲覧可能な範囲を守らせる
読了まで約64分。8 例。
この章を読み終えると、各ロールの行フィルタ、列マスク、最小グループサイズを、エージェントより下の層にある、ロール専用のデータベースで強制できるようになる。
講義概要: 10 節
-
A rule in the prompt is not a control
- 実行
prompt-rule-bypassed.py - 実践ルール Never enforce access in a prompt. Decide what a role may see in code, from the role your sign-in system gives you, and apply it to the SQL that runs. Keep access rules out of the prompt, so nobody mistakes it for the control, and test the control with questions that try every way around it: other regions by name, totals, rankings, and questions that claim an exception.
- 実行
-
Apply row filters in the compiler
- 実行
regional-lead-revenue.py - 実践ルール Attach row filters to tables, not to questions, and apply them by replacing each table a query reads with a filtered subquery, after the semantic layer compiles and after the guard checks. Write a filter for every table a filtered role can reach, reach the filter's column through a key where a table has none, and refuse tables with no filter. Refuse a filter value outside the role's rows, and record the row filter in every answer's provenance.
- 実行
-
すべての経路で列をマスクする
- 実行
masked-emails.py - 実践ルール 列をマスクするには、クエリが選択する列を書き換えるのではなく、テーブルを置き換える。ロールのデータベースにはビューを、書き換えにはサブクエリを使い、列の代わりにマスクを返す。個人情報の列はセマンティックレイヤーに入れない。ロールが存在を知ってはならない列は拒否し、存在は知ってよいが読んではならない列はマスクする。
- 実行
-
個人を特定できるほど小さいグループを抑制する
- 実行
small-group-suppressed.py - 実践ルール 人についての集計は見られるが人そのものは見られないロールには、すべて最小グループサイズを設定し、その理由とともに方針上の選択として記録する。統計機関は金額の表に3を用いており、それより大きな数にしても、回答間の差の取得は防げない。メトリクス自身のフィルタを用いてグループごとに重複を除いた人数を数え、最小値を下回るグループの値をすべて抑制し、行を個人に辿れないメトリクスは拒否する。
- 実行
-
二つの回答の差を取ると、あるグループが復元される
- 先に確認する Before you run the example, predict whether the policy lets the second answer through, and what subtracting the first answer from it gives.
- 実行
difference-two-answers.py - 実践ルール 小グループの秘匿は一つの回答を守るものとして扱い、複数の回答にわたって個人を守るものとは決して考えない。小グループが問題となる場面では、エージェントを事前にまとめて計画した内訳に限定するか、値に摂動を加える。そのうえで、同一人物による重なり合う質問がないか監査ログを確認する。
-
Treat text in the data as data
- 実行
injected-product-name.py - 実践ルール Treat every value the model reads as untrusted, and never let a model's prose be the only form of an answer. Return the table with its provenance, show a summary only after code has checked it against the table, and keep the role and the policy out of anything the model can write. Marking data as data in the prompt is not a control.
- 実行
-
ゴールデン質問でアクセスを測定する
- 実行
role-scope-score.py - 実践ルール ポリシーを変更するたびに、質問者のロールが指定されたゴールデン質問でスコアを測定し、禁止された質問を両方の分割に含め、拒否されたかどうかだけでなく、どのように拒否されたかを読むこと。ルールが適用されることをロールに伝えるのか、答えが存在しないことだけを伝えるのかを決め、すべての拒否で同じ言い方にそろえること。
- 実行
-
Log who asked what, and what ran
- 実行
audit-entries.py - 実践ルール Write one audit entry per question, from code, with the principal from your sign-in system, what the agent asked for, the SQL that ran after the policy, and the outcome. Keep values out of the log, protect its questions as personal data, and review it for overlapping questions and for declines a role keeps hitting.
- 実行
-
本番で何が問題になるか
- 実践ルール ロールはサインインシステムから束縛し、リクエストの引数からは決して取得しない。ポリシーファイルは CI でウェアハウスのテーブルに対してテストする。ポリシーの真実の源は一つに保ち、ディメンションの値はポリシーを通して一覧にする。ポリシーを変更するたびに、監査ログで拒否、単一グループの回答、重複する質問を確認する。
- この章で下した決定
10. 回答を評価し、リグレッションを検出する
読了まで約73分。7 例。
この章を終えると、次のことができるようになる。
講義概要: 8 節
-
Score a run on more than accuracy
- 実行
scorecard.py - 実行
figure-accuracy-intervals.py - 実践ルール Report every run as a scorecard: correct with a Wilson interval, wrong numbers, declines and their recall and precision, failures by kind, and calls and tokens per question. Compare agents on the same questions in the same table. Score the test split once, when you report a final number, and say that you did; when two agents' intervals overlap, don't call either better from the totals.
- 実行
-
Judge only what can't be compared
- 実行
judge-an-explanation.py - 実践ルール Use a judge only for what can't be compared with a result, such as reasons, explanations and questions back, and keep
results_match()for every number. Judge each pair in both orders and count the verdicts that change, record every verdict, and read a sample of the judge's reasons, not only its labels. Don't let a judge's preference between two texts decide a release until you have measured that judge on cases with known answers.
- 実行
-
Calibrate the judge against reference labels
- 実行
judge-agreement.py - 実践ルール Before you use a judge on text, measure it on cases whose answers a check can label, with labels anyone can recompute. Report observed agreement with Cohen's kappa and Scott's pi, split by the kinds of answer you'll judge, list the errors in each direction, and say how far one case moves kappa when the cases number a few dozen. Thresholds such as 0.8 are conventions, not results (Artstein and Poesio); at a kappa like the one measured here, use the judge to choose what a person reads, never to pass or fail a release on its own.
- 実行
-
Repeat runs to see the variance
- 実行
seed-variance.py - 実践ルール Run each golden question several times at the sampling settings you serve, with seeds pinned in the recording's manifest, and confirm once that a seed replays exactly. Report per-question flips, not only the spread of the total, because a total can stay put while questions trade places. Never lower the temperature to make runs agree, unless that is the setting you will serve.
- 実行
-
Gate a change in CI
- 実行
regression-gate.py - 実践ルール Gate a change to prompts, context or metrics on paired, per-question differences over repeated runs, never on a difference of totals. Fail on any stable break and on a mean difference whose interval lies below zero, write both rules down as your choice, and compute the smallest change they can detect on your set. When the gate fails, read the questions that moved before you touch the rules.
- 実行
-
Catch a schema change before users do
- 実行
renamed-column-caught.py - 実践ルール Run the golden SQL, every metric and every recorded replay in CI on changes to the warehouse as well as to the agent, and treat
MissingRecordingas a failure, never as a reason to skip a question. When a check fails after a schema change, fix the definition in the semantic layer before you record anything again.
- 実行
-
本番環境で何がうまくいかなくなるか
- 実践ルール ゲートが本番のエージェントを守る前に、判定器の較正ケースを判定器プロンプトと並べて保管し、どちらかが変わるたびに再計測する。判定器には自分で確認できるものを見せ、提供する設定で繰り返しの実行を記録し、ゲートの規則は数値に責任を持つ人々と合意し、各マニフェストに分割を録画した回数を数えて記す。
- この章で行った決定
11. MCP でセマンティックレイヤーを提供する
読了まで約56分。5 例。
この章を終えると、次のことができるようになる。
講義概要: 7 節
-
Turn metrics into tools
- 実行
list-metric-tools.py - 実践ルール Serve the tools your agent was measured with, each one a thin call into the same code the in-process agent uses, so that the server can't drift from what you tested. List them in a fixed order and mark them read-only. Let the role change what a tool returns, and shorten the tool list only when a role can't use a tool at all.
- 実行
-
呼び出し元のロールをすべてのクエリに引き継ぐ
- 実行
role-bound-at-build.py - 実践ルール ロールは、ツールの引数ではなく、その人が認証された場所で束縛する。stdio サーバでは、ホストが環境経由でロールを渡し、サーバはそのロールの読み取り専用データベースを開いてポリシーを一度だけ構築する。HTTP サーバでは、リクエストごとに、このサーバ向けに発行された検証済みトークンからロールを取得し、トークンのスコープが何を言っていても、そのロールの行をデータベースとポリシーで強制する。
roleをすべての入力スキーマから除外し、モデルや質問が埋められるものを残さないようにする。ツールが無視した引数は、とくにroleを最初に、ログに記録する。それは、モデルが読んだテキストがモデルを操作していることの兆候だからである(チケットのテキストを信頼できないデータとして扱う)。
- 実行
-
回答を出所情報とともに返す
- 実行
tool-result-with-provenance.py - 実践ルール すべてのツールから pydantic モデルを返し、各回答が行、役割、フィルター、データの鮮度とともに構造化コンテンツとして運ばれ、ホストのコードがそれらを検査できるようにする。モデルが修正できる要求にはツールエラーを送出し、修正の方法を示すメッセージを付ける。アクセスに対する拒否は、理由を添えた辞退の回答として返し、モデルに別の経路を試させず、ホストが実行を終わらせるようにする。
- 実行
-
サーバーをメモリ内でテストする
- 実行
semantic-server-tests.py - 実践ルール 各役割のルールは、役割ごとに一つのサーバーを用意し、メモリ内クライアントを通じて、構造化された応答に対するアサーションでテストする。その際、バインディングが誤っているときに失敗する検査も含める。期待値にはゴールデンクエスチョンの結果を用い、テストとスコアが何を正解とするかについて一致するようにする。メモリ内クライアントが送れないものについては、トークンを最優先に、HTTP 経由のテストを追加する。
- 実行
-
Answer questions through a host
- 実行
host-asks-the-server.py - 実践ルール Put the server under the host you already measured, and score it on the golden questions before anyone uses it, because the transport can change what the model reads. Compare outcomes question by question with the in-process runs, and read every run that changed. Record what the new text produces rather than editing the server's messages to match old recordings, and show people the structured answer with its provenance.
- 実行
-
本番環境で起こる問題
- 実践ルール stdio サーバーは 1 人につき 1 つ動かすか、各リクエストのトークンからロールを取得する HTTP を使い、ポリシーファイルが知らないクレームはすべて安全側に倒して失敗させる。クエリには共有のデータベースではなく、そのロール専用の読み取り専用データベースを使う。SDK を固定し、変わるたびにホスト経由でゴールデン質問を再実行する。各ホストにはセマンティックレイヤーごとに 1 つのサーバーだけを置き、すべての応答について監査エントリを記録し、複数の人が共有しているように見えるプリンシパルがないか、それを読んで確認する。
- この章で下した決定
問題バンク
全章の設問と解答を、認証済み教育者向けに提供する。各解答は該当する節に紐づいている。 入手方法
問題集を読み込み中…