This is a post from Robin Sloan’s lab blog & notebook. You can visit the blog’s homepage, or learn more about me.

Generating product SKUs with Claude

July 14, 2025

Here’s a nice little thing I did with Claude recently. I have a few hun­dred books in my Shopify store, all different. Entering each book, I took a picture, tran­scribed the title, added it to a col­lection, pressed save — and on to the next.

Then, I dis­cov­ered that I wanted SKUs, too — concise slugs — for the mini man­i­fests printed on my ship­ping labels. (There isn’t room to include the com­plete titles.)

As a task, this is sort of the worst mix of repet­i­tive and fiddly, so, I wrote the fol­lowing code, which, I am pleased to report, did actu­ally take less time than the repet­i­tive/fiddly task would have — never a guarantee:

require "anthropic"

ANTHROPIC = Anthropic::Client.new(
  api_key: CLAUDE_API_KEY
)

def generate_sku(title_and_author)
  prompt = <<~PROMPT
    I'm going to give you the title and author of a book. I need you to generate a SKU code for this book, following these rules:

    * If the title is very short, the SKU should be TITLE-AUTHORLASTNAME
    * If the title is medium-length or long, the SKU should use a key word from the title, and it should be WORD-AUTHORLASTNAME
    * If the book has multiple authors, use the last name of the first listed author

    Please reply with ONLY the SKU, without any additional text or commentary.

    Here is the title and author:

    #{title_and_author}
  PROMPT

  response = ANTHROPIC.messages.create(
    max_tokens: 64,
    messages: [{role: "user", content: prompt}],
    model: "claude-sonnet-4-20250514",
    temperature: 0.7
  )

  return response.content[0][:text]
end

items = get_items_from_shopify # not included in this code listing

items.each do |item|
  sku = generate_sku(item[:title_and_author])
  apply_sku(item, sku) # not included in this code listing
end

Pretty nice!

To the blog home page