Secret Ninja Blog

プロダクトマネージャーしてます

AI NativeなtdxコマンドとClaude Codeを使ってCDPの運用を効率的に行う

Treasure DataのR&Dチームには、DevAIユニットがあり、社内外に向けプロダクトや周辺ツールをAI-Native化をする取り組みを行っています。

例えば、過去にはTreasure DataのMCPサーバーも公開しました。

github.com

さらに、今回新しいtdxコマンドをリリースしました。

tdx.treasuredata.com

これはAIエージェントとプラットフォームをつなぐ主要なインターフェイスとして位置づけとなっており、MCP 経由で API を直接扱う際に起きがちな「コンテキストが足りない」「トークン消費が多い」といった課題を解消し、より安定した・効率的な操作性を実現でき、また、これまで個別に存在していたスクリプトやTips、運用テクニックをtdx CLI と td-skills形式のプロンプトへ段階的に統合していくことで、運用・ナレッジの集約ができたり、tdx を利用することで、顧客ごとのユースケースに合わせた AIエージェントの構築が圧倒的にスピードアップし、AI + CLI で再現性をもって自動化できる世界になっていきます。

では実際に試してみましょう!

利用方法:

例:ワークフローをデバッグして修正する

tdxにはデフォルトでTreasure Dataに関連したスキルが備わっており、下記のように明示することで使うことができます。

"Use the Trino skill to extract data from sample_datasets.nasdaq table"
"Use the Hive skill to write a query for daily user aggregation"
"Use the Trino CLI skill to help me connect to TD from the terminal"
"Use the TD MCP skill to set up Claude Code to access my TD databases"
"Use the activations skill to query activation logs for parent segment 394649"
"Use the digdag skill to create a workflow that runs every morning"
"Use the workflow-management skill to debug this failing workflow"
"Use the dbt skill to create an incremental model for user events"
"Use the JavaScript SDK skill to implement event tracking on my website"
"Use the pytd skill to query TD from Python and load results into pandas"
"Use the tdx-basic skill to list all databases in the Tokyo region"
"Use the tdx skill to query the users table and save results as JSON"
"Use the deployment skill to set up a production publishing workflow"
"Use the documentation skill to create comprehensive Field Agent documentation"
"Use the visualization skill to create a Plotly chart with TD colors"

また、下記コマンドでClaude Codeに必要な初期設定をバイパスして起動することも可能です。

tdx claude

例えばとあるTreasure Workflowの処理が失敗していたとします。Claude Codeで下記のように依頼することで、自動でログやプロジェクトファイルを参照し、原因を分析します。

> Use the workflow-management skill to debug this failing workflow 
https://console.us01.treasuredata.com/app/workflows/37912792/sessions/304847564/attempt/1357440936 

⏺ I'll help you debug this failing workflow using the workflow-management skill.

> The "workflow-management" skill is loading 

⏺ Now I'll debug the failing workflow. Let me start by getting details about this attempt and identifying what failed.

⏺ Bash(tdx wf attempt 1357440936)
  ⎿  Running…

  Todos
  ☐ Retrieve attempt details for workflow attempt 1357440936
  ☐ Analyze task execution to identify the failure
  ☐ Review logs for the failed task
  ☐ Provide root cause analysis and solution

最終的には解決策の提案を行い、どの修正案を適用するかを選ぶと、実際に修正の反映まで含めて適用をしてくれます。

⏺ Root Cause Analysis

  I've identified the issue with your workflow. The failure occurs in task +receive_purchasers+count_table at line 11-13 of activation_testing.dig:11-13.

  The Problem:

  The query queries/2_count_new_purchasers.sql uses the variable ${recent_purchasers} on lines 2 and 6:

  SELECT COUNT(*) as counter
    ,(SELECT COUNT(*) FROM ${recent_purchasers}) <= (SELECT COUNT(*) FROM new_barcodes) as checker
    ...
    FROM ${recent_purchasers}

  However, this variable is not defined in the workflow's export scope.

  Current Config Structure:

  Your config.yml defines the table name nested under tables:

  tables:
    recent_purchasers: new_recent_purchasers

  When included via !include: 'config.yml', the variable path becomes ${tables.recent_purchasers}, not ${recent_purchasers}.

  Solution

  You have two options to fix this:

  Option 1: Update the Config (Recommended)

  Flatten the config structure to export variables at the top level:

⏺ Update(activation_testing/config.yml)
  ⎿  Updated activation_testing/config.yml with 6 additions
        1    database: demo
        2    
        3 +  # Direct variable exports for use in queries
        4 +  recent_purchasers: new_recent_purchasers
        5 +  barcode: new_barcodes
        6 +  promo_code: new_promo_codes
        7 +  
        8 +  # Nested table references (if needed elsewhere)
        9    tables:
       10      barcode: new_barcodes
       11      promo_code: new_promo_codes

他にも

また、tdxにはAudience Studioのリソースを操作するためのオペレーションもサポートされており、CDPのオペレーション自体もスキル化することでAIによって自動化できます。今後どんどんこうした連携が強化されていく予定です!

Usage: tdx [options] [command]

AI-native CLI for Treasure Data, version 0.4.1 (6e2bc3d)

Commands:
  databases [pattern]         List all databases (optionally filtered by glob pattern)
  chat [message]              Chat with an LLM agent (simplified interface)
  api [endpoint]              Make raw HTTP requests to TD APIs
  tables [pattern]            List tables (e.g., mydb, mydb.*, *.user*, mydb.user*)
  show [table]                Show table contents (SELECT * with limit)
  describe, desc [table]      Describe table schema
  query [sql]                 Run Trino query with streaming results
  segments [path]             List folders and segments at path or in current context
  segment                     CDP segment management commands
  activations <segment_name>  List activations for a segment (use parent_name/child_name)
  workflow, wf                Workflow (Digdag) management commands
  jobs                        List jobs
  job                         Job management commands
  llm                         LLM operations (models, projects, agents, chat)
  claude [args]               Launch Claude Code with integrated LLM proxy (use -- to pass options to Claude Code)
  context                     Show current resolved context (use --debug to show sources)
  use [resource] [value]      Set session-level context (resources: database, parent_segment, llm_project, llm_agent, profile)
  auth                        Authentication management
  profiles                    List all available profiles